#π Object detector
20 messages Β· Page 1 of 1 (latest)
@graceful halo
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what do you call an object detector ?
Show us what you tried so we can understand better your question
I deleted the code in rage but the main problem was the libraries and python version
-> i downloaded latest python version and then tried to download tensorflow which is not compatible with latest version so i downgraded the version
and
then tried the files of yolo were corrupted so i manually downloaded and replaced them but it still wasn't working the cvlib even though after specifying the files was trying to download files called yolo_v3 smth and the code was not working so I removed cvlib
and
tried it again but coco.names file had some problem I tried many times and tried to anything i could do to make it work but it didn't so i deleted the files and manually added that in the code but it still wasn't working so I rage quit.
I still have the code tho
||import cv2
import numpy as np
import os
--- Hardcoded COCO class names ---
class_names = [
'person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
'sofa', 'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor',
'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]
--- Paths to cfg and weights (same folder as script) ---
base_path = os.path.dirname(file)
cfg_path = os.path.join(base_path, "yolov3-tiny.cfg")
weights_path = os.path.join(base_path, "yolov3-tiny.weights")
--- Load YOLO network ---
net = cv2.dnn.readNet(weights_path, cfg_path)
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
--- Load Haar cascade for face detection ---
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')||
||# --- Distance estimation settings ---
KNOWN_WIDTH = 40 # cm, approximate width of a person
FOCAL_LENGTH = 700 # adjust if needed
--- Open laptop webcam ---
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
ret, frame = cap.read()
if not ret:
break
height, width, _ = frame.shape
# --- Face detection ---
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, "Face", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# --- YOLO object detection ---
blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
class_ids = []
confidences = []||
||boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# Non-max suppression
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
for i in indexes.flatten():
x, y, w, h = boxes[i]
label = class_names[class_ids[i]]
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Distance estimation for person
if label == 'person' and w > 0:
distance = (KNOWN_WIDTH * FOCAL_LENGTH) / w
cv2.putText(frame, f"{label} {int(distance)} cm", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
else:
cv2.putText(frame, label, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
cv2.imshow("Face + YOLOv3-tiny Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()||
The code is hidden
!pastebin
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
Please use the pastebin, makes it easier for people to help you.
this is the code
and the problem
!close
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.