Hamza011 commited on
Commit
024a0ce
·
verified ·
1 Parent(s): 15c93ba

Create app1.py

Browse files
Files changed (1) hide show
  1. app1.py +83 -0
app1.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+ # Load the SSD model and configuration
5
+ model_path = 'ssd_mobilenet_v2_coco.pb' # Path to the pre-trained SSD model
6
+ config_path = 'deploy.prototxt' # Path to the deploy prototxt file
7
+
8
+ # Load the class labels from the COCO dataset
9
+ CLASSES = [
10
+ 'background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
11
+ 'truck', 'boat', 'traffic light', 'fire hydrant', 'none', 'stop sign', 'parking meter',
12
+ 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
13
+ 'giraffe', 'none', 'backpack', 'umbrella', 'none', 'handbag', 'tie', 'suitcase',
14
+ 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
15
+ 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'none', 'wine glass', 'cup',
16
+ 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli',
17
+ 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant',
18
+ 'bed', 'dining table', 'toilet', 'none', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
19
+ 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
20
+ 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
21
+ ]
22
+
23
+ # Initialize the OpenCV DNN network
24
+ net = cv2.dnn.readNetFromTensorflow(model_path, config_path)
25
+
26
+ # Function to process the video frame and detect objects
27
+ def detect_objects_in_frame(frame):
28
+ # Get the image shape
29
+ height, width = frame.shape[:2]
30
+
31
+ # Prepare the frame for the model (mean subtraction and resizing)
32
+ blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (127.5, 127.5, 127.5), swapRB=True, crop=False)
33
+
34
+ # Set the blob as input to the network
35
+ net.setInput(blob)
36
+
37
+ # Run the forward pass to get predictions
38
+ detections = net.forward()
39
+
40
+ # Loop through all the detections
41
+ for i in range(detections.shape[2]):
42
+ confidence = detections[0, 0, i, 2]
43
+
44
+ if confidence > 0.5: # Set a threshold for object detection
45
+ # Get the class index and the bounding box coordinates
46
+ class_id = int(detections[0, 0, i, 1])
47
+ left = int(detections[0, 0, i, 3] * width)
48
+ top = int(detections[0, 0, i, 4] * height)
49
+ right = int(detections[0, 0, i, 5] * width)
50
+ bottom = int(detections[0, 0, i, 6] * height)
51
+
52
+ # Draw the bounding box and label on the frame
53
+ label = f"{CLASSES[class_id]}: {confidence:.2f}"
54
+ cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
55
+ cv2.putText(frame, label, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
56
+
57
+ return frame
58
+
59
+ # Capture video from a file or camera (0 for the default camera)
60
+ cap = cv2.VideoCapture(0) # Use 0 for webcam or provide a path to a video file
61
+
62
+ # Check if the video capture is initialized correctly
63
+ if not cap.isOpened():
64
+ print("Error: Could not open video stream.")
65
+ exit()
66
+
67
+ while True:
68
+ # Read a new frame from the video feed
69
+ ret, frame = cap.read()
70
+
71
+ if not ret:
72
+ print("Error: Failed to read frame from video stream.")
73
+ break
74
+
75
+ # Detect objects in the current frame
76
+ output_frame = detect_objects_in_frame(frame)
77
+
78
+ # Display the resulting frame
79
+ cv2.imshow("Detected Objects in Video", output_frame)
80
+
81
+ # Break the loop if the user presses the 'Esc' key
82
+ if cv2.waitKey(1) & 0xFF == 27: # 27 is the keycode for 'Esc'
83
+ break