detech humans in video using cctv opencv
- User
detech humans in video using cctv opencv
- Assistant
- User
Traceback (most recent call last): File "/Users/macbook/fun/sih/rhtd/src/exp/main.py", line 6, in output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/macbook/fun/sih/rhtd/src/exp/main.py", line 6, in output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] ~^^^ IndexError: invalid index to scalar variable.
- Assistant
The error message you're encountering, "IndexError: invalid index to scalar variable," typically occurs when there is an issue with accessing elements in a list or array. In this case, it seems to be related to the line of code where you're defining
output_layers.The
net.getUnconnectedOutLayers()function returns the indices of the unconnected output layers in the neural network model. If you're encountering this error, it's possible that there is an issue with the YOLO configuration file () or the weights file (). - User
IndexError: invalid index to scalar variable.
- Assistant
The "IndexError: invalid index to scalar variable" error typically occurs when you are trying to access an index of an object that is not subscriptable. In the code you provided, the error occurs on this line:
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]To troubleshoot this issue, follow these steps:
-
Check YOLO Configuration: Ensure that you are using the correct YOLO configuration file (
yolov3.cfg) and weights file () that match each other. If there is a mismatch between the configuration and weights, it can lead to this error.
-
- User
I just had to remove the I[I]
- Assistant
I see, it seems like you've resolved the issue by removing the
I[I]indexing. That's great to hear! The corrected line should look like this:output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]By making this change, you should no longer encounter the "IndexError: invalid index to scalar variable" error, and your code should work as expected for detecting humans in video using YOLO and OpenCV. If you have any more questions or need further assistance, feel free to ask.
- User
it shows multiple borders on humans tho how can fix that
- Assistant
To fix the issue of multiple borders around detected humans, you can modify the code to perform non-maximum suppression (NMS) on the detected bounding boxes. NMS is a post-processing step that removes duplicate or overlapping bounding boxes, keeping only the most confident ones.
Here's how you can incorporate NMS into your code:
- Import the
cv2.dnn.NMSBoxesfunction:
import cv2.dnn.NMSBoxes - Import the
- User
gib me the full script pls ty
- Assistant
Certainly! Here's the full script for detecting humans in a video using YOLO and OpenCV with non-maximum suppression (NMS):
import cv2 import numpy as np # Load YOLO model net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] # Open video capture (use 0 for webcam, or provide the path to the video file) cap = cv2.VideoCapture('cctv_video.mp4') # List to store selected detections selected_detections = [] while True: ret, frame = cap.read() if not ret: break height, width, channels = frame.shape # Detecting objects blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers) # Process detected objects for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5 and class_id == 0: # Class ID 0 corresponds to humans in COCO dataset # Object detected as a human, draw bounding box 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) selected_detections.append([x, y, x + w, y + h, confidence]) # Apply Non-Maximum Suppression indices = cv2.dnn.NMSBoxes(selected_detections, confidence_threshold=0.5, nms_threshold=0.4) # Draw bounding boxes for the selected detections after NMS for i in indices: i = i[0] x, y, x_plus_w, y_plus_h, conf = selected_detections[i] cv2.rectangle(frame, (x, y), (x_plus_w, y_plus_h), (0, 255, 0), 2) cv2.imshow("CCTV Feed", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()