-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88f2ca3
commit 512fc77
Showing
3 changed files
with
44 additions
and
264 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,68 @@ | ||
import io | ||
import streamlit as st | ||
import cv2 | ||
import telebot | ||
import requests | ||
import numpy as np | ||
import io | ||
|
||
# TODO: 1.1 Get your environment variables | ||
chat_id='' | ||
bot_id = '' | ||
channel_id="@amrita_eco" | ||
# Initialize Telegram bot | ||
bot_id = st.secrets["bot_id"] | ||
chat_id = st.secrets["chat_id"] | ||
channel_id = st.secrets["channel_id"] | ||
url = f"https://api.telegram.org/bot{bot_id}/sendPhoto" | ||
bot = telebot.TeleBot(bot_id) | ||
start_message = "Hello, I'm your bot and I'm starting up!" | ||
bot.send_message(chat_id, start_message) | ||
|
||
# Custom categories | ||
category_map = { | ||
'beasts': ['bear', 'elephant', 'zebra', 'giraffe','dog', 'cat','cow','sheep'], | ||
'birds': ['bird'], | ||
'person': ['person'], | ||
'vehicle': ['bicycle','car','motorcycle','bus','truck','train','boat'], | ||
'signs':['street sign','stop sign'], | ||
'cutting_objects':['knife','fork'], | ||
'danger': ['snake', 'spider'] | ||
} | ||
|
||
# Function to map the COCO class to its category | ||
def map_to_category(class_name): | ||
for category, classes in category_map.items(): | ||
if class_name in classes: | ||
return category | ||
return 'other' # Return 'other' if the class doesn't belong to any custom category | ||
# Helper function to send photos to Telegram | ||
def sendbot(img, message): | ||
image_bytes = cv2.imencode(".jpg", img)[1].tobytes() | ||
with io.BytesIO(image_bytes) as f: | ||
f.name = "image.jpg" | ||
media_photo = telebot.types.InputFile(f) | ||
bot.send_photo(chat_id, media_photo) | ||
data = {"chat_id": channel_id, "text": message} | ||
requests.post(url, data=data, files={"photo": f}) | ||
|
||
# Load the COCO class names | ||
classNames = [] | ||
classFile = 'coco.names' | ||
with open(classFile, 'rt') as f: | ||
classNames = f.read().rstrip('\n').split('\n') | ||
|
||
# Load model configuration and weights | ||
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' | ||
weightsPath = 'frozen_inference_graph.pb' | ||
|
||
net = cv2.dnn_DetectionModel(weightsPath, configPath) | ||
net.setInputSize(320, 320) | ||
net.setInputScale(1.0/127.5) | ||
net.setInputScale(1.0 / 127.5) | ||
net.setInputMean((127.5, 127.5, 127.5)) | ||
net.setInputSwapRB(True) | ||
print("Press backspace or ESC to close the cam") | ||
# Open the camera | ||
cap = cv2.VideoCapture(0) # Change the argument to the camera index if you have multiple cameras | ||
|
||
while True: | ||
ret, img = cap.read() # Read a frame from the camera | ||
# Streamlit UI and camera handling | ||
st.title("Real-time Object Detection and Alerts") | ||
st.write("Capture an image using the camera below:") | ||
|
||
# Capture image from the browser | ||
image_data = st.camera_input("Camera") | ||
|
||
if image_data is not None: | ||
# Convert the image from Streamlit format to OpenCV format | ||
file_bytes = np.asarray(bytearray(image_data.read()), dtype=np.uint8) | ||
img = cv2.imdecode(file_bytes, 1) | ||
|
||
# Perform object detection | ||
classIds, confs, bbox = net.detect(img, confThreshold=0.5) | ||
|
||
if len(classIds) != 0: | ||
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox): | ||
class_name = classNames[classId-1] | ||
category = map_to_category(class_name) | ||
# Set color based on category | ||
if (category == 'beasts')or(category=='danger')or(category=='cutting_objects'): | ||
color = (0,0 ,255) # red (for beasts & dangerous things) | ||
if(category=='cutting_objects'): | ||
message="Alert! Sharp objects detected Detected" | ||
bot.send_message(chat_id, message) | ||
else: | ||
message="Alert! Beast Detected" | ||
bot.send_message(chat_id, message) | ||
image_bytes = cv2.imencode(".jpg", img)[1].tobytes() | ||
|
||
# Wrap the byte array in a file-like object | ||
with io.BytesIO(image_bytes) as f: | ||
# Set the name attribute of the file-like object | ||
f.name = "image.jpg" | ||
|
||
# Create an InputFile object | ||
media_photo = telebot.types.InputFile(f) | ||
|
||
# Send the photo to the user | ||
bot.send_photo(chat_id, media_photo) | ||
data = { | ||
"chat_id": channel_id, | ||
"text": message | ||
} | ||
cv2.imwrite("image.jpg", img) | ||
media_photo = {"photo": ("image.jpg", open("image.jpg", "rb"))} | ||
response = requests.post(url, data=data,files=media_photo) | ||
elif category == 'birds': | ||
color = (0, 255, 0) # (for birds) | ||
elif category == 'person': | ||
color = (0, 255, 255) # Yellow (for persons) | ||
elif (category == 'vehicle') or (category=="signs"): | ||
color = (255, 0, 0) # Blue (for vehicle & signs) | ||
else: | ||
color = (255, 255,255) # White (for other objects) | ||
|
||
cv2.rectangle(img, box, color=color, thickness=3) | ||
cv2.putText(img, category, (box[0]+10, box[1]+30), cv2.FONT_HERSHEY_COMPLEX, 1, color, 2) | ||
|
||
cv2.imshow('Object Detection', img) # Show the image | ||
|
||
# Wait for 'ESC' or 'Backspace" key to be pressed to end the program | ||
tecla = cv2.waitKey(5) & 0xFF | ||
if ((tecla == 27) or (tecla==8)): | ||
break | ||
|
||
cap.release() # Release the camera | ||
cv2.destroyAllWindows() | ||
bot.send_message(chat_id, "Bye Bye Shutting down") | ||
bot.polling() | ||
|
||
# Detect and categorize objects | ||
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox): | ||
class_name = classNames[classId - 1] | ||
# Customize colors and labels based on the category | ||
color = (0, 255, 0) if class_name == "person" else (0, 0, 255) # Green for persons, red for others | ||
cv2.rectangle(img, box, color=color, thickness=3) | ||
cv2.putText(img, class_name, (box[0] + 10, box[1] + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2) | ||
|
||
# Display detection results in Streamlit | ||
st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption="Detected Objects", use_column_width=True) | ||
|
||
# Send alert to Telegram if specific objects are detected | ||
if "person" in [classNames[id - 1] for id in classIds.flatten()]: | ||
sendbot(img, "Alert! Person Detected") |
177 changes: 0 additions & 177 deletions
177
22AIE114_Project_codes_A12/YOLO(Python)/main_updated.py
This file was deleted.
Oops, something went wrong.