Skip to main content

sdsfd

from ultralytics import YOLO

import cv2

import BlynkLib

import time

from datetime import datetime

import requests


# Blynk Configuration

BLYNK_AUTH = "YOUR_BLYNK_AUTH_TOKEN"  # Replace with your Blynk auth token

BLYNK_SERVER = "blynk.cloud"


# Initialize Blynk

blynk = BlynkLib.Blynk(BLYNK_AUTH)


# Virtual Pins

VPIN_ALERT = 0          # V0 - Alert text display

VPIN_DETECTION_LED = 1  # V1 - LED indicator

VPIN_LAST_SEEN = 2      # V2 - Last detection timestamp

VPIN_ANIMAL_COUNT = 3   # V3 - Number of animals detected

VPIN_IMAGE = 4          # V4 - Image widget for detection snapshots


# Load YOLO model

model = YOLO("yolov8n.pt")


# Animal classes to detect

animal_classes = {

    0: "person",

    14: "bird",

    15: "cat",

    16: "dog",

    17: "horse",

    18: "sheep",

    19: "cow",

    20: "elephant",

    21: "bear"

}

animal_ids = list(animal_classes.keys())


# Initialize webcam (USB webcam on Raspberry Pi)

cap = cv2.VideoCapture(0)


# Set camera resolution (lower for better performance on Pi)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)


print("Animal Detection System Started on Raspberry Pi")

print("Press Ctrl+C to exit")


last_alert_time = 0

alert_cooldown = 2  # Send Blynk alert every 2 seconds max


def send_image_to_blynk(frame):

    """Send captured image to Blynk when animal detected"""

    try:

        # Encode frame to JPEG with compression for faster upload

        _, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 70])

        

        # Save temporarily

        filename = "/tmp/detection.jpg"

        with open(filename, 'wb') as f:

            f.write(buffer)

        

        # Upload to Blynk

        url = f"https://{BLYNK_SERVER}/external/api/upload/image"

        with open(filename, 'rb') as img_file:

            files = {'image': img_file}

            data = {'token': BLYNK_AUTH, 'pin': f'V{VPIN_IMAGE}'}

            response = requests.post(url, files=files, data=data, timeout=5)

        

        if response.status_code == 200:

            print("  ✓ Image uploaded to Blynk")

        else:

            print(f"  ✗ Failed to upload image: {response.status_code}")

    except Exception as e:

        print(f"  ✗ Error uploading image: {e}")


try:

    while True:

        # Run Blynk

        blynk.run()

        

        ret, frame = cap.read()

        if not ret:

            print("Failed to grab frame from webcam")

            time.sleep(1)

            continue


        # Run YOLO detection

        results = model(frame, classes=animal_ids, verbose=False)


        intruder_detected = False

        detected_labels = []


        # Process detections

        for box in results[0].boxes:

            cls_id = int(box.cls[0])

            if cls_id in animal_classes:

                intruder_detected = True

                detected_labels.append(animal_classes[cls_id])


        # Handle detection alerts

        current_time = time.time()

        if intruder_detected:

            detected_labels = list(set(detected_labels))

            alert_text = "INTRUDER: " + ", ".join(detected_labels)

            animal_count = len(detected_labels)

            

            # Send to Blynk (with cooldown to avoid spam)

            if current_time - last_alert_time > alert_cooldown:

                blynk.virtual_write(VPIN_ALERT, alert_text)

                blynk.virtual_write(VPIN_DETECTION_LED, 255)  # Turn on LED

                blynk.virtual_write(VPIN_ANIMAL_COUNT, animal_count)

                blynk.virtual_write(VPIN_LAST_SEEN, datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

                blynk.log_event("animal_detected", alert_text)  # Create "animal_detected" event in Blynk

                

                # Send snapshot to Blynk

                send_image_to_blynk(frame)

                

                last_alert_time = current_time

                

            print(f"[{datetime.now().strftime('%H:%M:%S')}] {alert_text}")

        else:

            # No detection - turn off LED

            blynk.virtual_write(VPIN_DETECTION_LED, 0)

            blynk.virtual_write(VPIN_ALERT, "No intruders")

            blynk.virtual_write(VPIN_ANIMAL_COUNT, 0)


        # Small delay to reduce CPU usage

        time.sleep(0.1)


except KeyboardInterrupt:

    print("\nShutting down...")


finally:

    cap.release()

    print("Camera released. Exiting.") 

Comments

Popular posts from this blog

ലോകത്തിലെ ഏറ്റവും വലിയ ഉപ്പ് മരുഭൂമി- റൻ ഓഫ് കച്ച് |Largest salt desert in the world in Malayalam| Yathrakal ishtam

  ലോകത്തിലെ തന്നെ ഏറ്റവും വലിയ ഉപ്പ് മരുഭൂമി ഏതെന്ന് ചോദിച്ചാൽ ഒരൊറ്റ ഉത്തരമേയുള്ളൂ, അതാണ് റൻ ഓഫ് കച്ച്. ഗുജറാത്തിലെ കച്ച് ജില്ലയിലാണ് ഇത് സ്ഥിതി ചെയ്യുന്നത്. 2500 ചതുരശ്ര കിലോമീറ്ററാണ് ഈ ഉപ്പ് ഒരുഭൂമിയുടെ ആകെ വിസ്തീർണ്ണം.      ഒരു കാലത്ത് കടൽ കയറി കിടന്ന ഒരു ഇടമായിരുന്നു റൻ ഓഫ് കച്ച്. കാലക്രമേണ കടൽ പിൻവാങ്ങുകയും അവശേഷിച്ച കടൽജലം വറ്റിപ്പോകുകയും പിന്നീട് ഉപ്പ് രൂപം കൊള്ളുകയും ചെയ്തു. എല്ലാ മഴക്കാലത്തും അവിടെ മഴവെള്ളം കെട്ടിക്കിടന്ന് തടാകം രൂപപ്പെടുകയും, മഴക്കാലം അവസാനിക്കുമ്പോൾ ഉപ്പ് അവിടെ രൂപം കൊള്ളും.     സഞ്ചാരികളെ ഇവിടേക്ക് ആകർഷിക്കുന്ന ഒരു പരുപാടിയാണ് റൻ ഉദ്സവ്. ഗുജറാത്ത് ടൂറിസമാണ് ഈ പരുപാടി നടത്തുന്നത്. വർഷവും 3 മാസത്തോളം നീണ്ടു നിൽക്കുന്ന റൻ ഉദ്സവ് ഡിസംബർ മുതൽ ഫെബ്രുവരിവരെയാണ് നടത്തപ്പെടുന്നത്. 400 ൽ പരം കൂടാരങ്ങളിൽ താമസത്തിനും  മറ്റുമുള്ള സൗകര്യവും ഭക്ഷണശാലയും അടങ്ങിയിട്ടുണ്ട്. പരമ്പരാഗത നൃത്തം, സംഗീതം, കരകൗശലവിദ്യ, എന്നിവയാണ് ഇവിടെ ചിത്രീകരിക്കുന്നത്. ഇതിനെല്ലാം പുറമെ ഒരുപാടത്തികം സാഹസിക പരിപാടികളും ഇവിടെ നടത്തപ്പെടുന്നു. ഗുജറാത്തിൽ വരുന്...

ഇന്ത്യയുടെ ആത്മീയ തലസ്ഥാനം-ഋഷികേശ് |Yathrakal ishtam| spiritual capital of India Rishikesh in Malayalam

Kedarnath temple Photo courtesy insta@ keraliann വടക്കൻ ഉത്തരാഖണ്ഡിലെ ഒരു നഗരമാണ് ഋഷികേശ്. ഹിമാലയൻ മലനിരകൾ അതിരിട്ടു നിൽക്കുന്ന താഴ് വരയിൽ പുണ്ണ്യനദിയായ ഗംഗയുടെ തീരത്താണ് ഋഷികേശ് സ്ഥിതി ചെയ്യുന്നത്. നൂറിലധികം ക്ഷേത്രങ്ങളും യോഗ കേന്ദ്രങ്ങളും, പ്രകൃതി മനോഹാരിതയുമാണ് ഈ സ്ഥലത്തെ സവിശേഷമാക്കുന്നത്     കേദാർനാഥ്, ബദരീനാഥ്, ഗംഗോത്രി, യമുനോത്രി എന്നീ ക്ഷേത്രങ്ങളിലേക്കുള്ള  യാത്ര ആരംഭിക്കുന്നത് ഇവിടെ നിന്നാണ്. പണ്ട് കാലത്ത് ഋഷിവര്യന്മാർ ശാന്തമായ ഗംഗയുടെ തീരത്ത് തപസ്സ് ചെയ്യതിരുന്നതായി പറയപ്പെടുന്നു. യോഗ കാപ്പിറ്റൽ എന്ന് അറിയപ്പെടുന്നത് ഋഷികേശാണ്. ഒരുപാടത്തികം യോഗ കേന്ദ്രങ്ങൾ ഇവിടെ പ്രവർത്തിച്ചുവരുന്നുണ്ട്. ആദ്യകാലത്ത് ആത്മീയത തേടിയായിരുന്നു ഏറ്റവുമധികം ആളുകൾ ഇവിടെ എത്തിച്ചേരുന്നത്. ഇപ്പോൾ ആത്മീയതക്ക് പുറമെ ടൂറിസവും പച്ച പിടിച്ചിട്ടുണ്ട്.     ഗംഗയിലുടെയുള്ള റിവർ റാഫ്ടിംഗ്, പാരാഗ്ലൈടിംഗ്, കായാക്കിങ്, ബൻജി ജംമ്പിങ്,  ക്യാമ്പിങ്. ഇത്തരത്തിലുള്ള നിരവധി സാഹസിക പ്രവർത്തനങ്ങൾ സഞ്ചാരികളെ ആകർഷിക്കുന്നുണ്ട്. 

മൂന്നാർ ഹെഡ് വർക്ക്സ്അണക്കെട്ട് || headworks dam munnar in Malayalam

കിഴക്കിന്റെ  കാശ്മീരായ മൂന്നാറിലെക്ക് എത്തുന്ന നമ്മളെ സ്വീകരിക്കാനായി റോഡിന്റെ വലതു വശത്ത് നിൽക്കുന്നു മൂന്നാർ ഹെഡ് വർക്ക്സ് അണക്കെട്ട്.പെരിയാറിന്റെ പോഷക നദിയായ മുതിരപ്പുഴയാറിലാണ് ഈ അണക്കെട്ട്. സർ. സി.പി. രാമസ്വമി അയ്യർ ഹെഡ് വർക്ക്സ് അണക്കെട്ട് എന്നും ഇതറിയപ്പെടുന്നു. കുണ്ടള, മാട്ടുപ്പെട്ടി ഡാമുകളിൽ നിന്നും വരുന്ന വെള്ളത്തിന്റെ അളവ് നിയന്ത്രിച്ച് പള്ളിവാസൽ ജലവൈദ്യുത പദ്ധതിയിലേക്ക് വിടുന്നത് ഹെഡ് വർക്ക്സ് അണക്കെട്ടാണ്.   1944 -ൽ ആണ് ഈ അണക്കെട്ടിന്റെ പണി പൂർത്തിയായത് . ഹെഡ് വർക്ക്സ് അണക്കെട്ടിനോട് ചേര്‍ന്ന് നിൽക്കുന്ന മൂന്നാർഹൈഡൽ പാര്‍ക്കും തദ്ദേശീയരും വിദേശീയരുമായ വിനോദ സഞ്ചാരികളെ ആകർഷിക്കുന്നു.