Skip to content

Commit 23aa3f8

Browse files
authored
object_detection
1 parent 4073901 commit 23aa3f8

File tree

5 files changed

+8629
-0
lines changed

5 files changed

+8629
-0
lines changed

OpenCV Projects/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Object Detection with OpenCV and SSD MobileNet v3
2+
## Description
3+
![Screenshot 2024-06-19 121308](https://github.com/nishant4500/object-detection/assets/135944619/44175bfa-25f2-40e5-8f36-297ff2517fba)
4+
![Screenshot 2024-06-19 121106](https://github.com/nishant4500/object-detection/assets/135944619/158f42c7-2c72-4c48-b0dc-9bc55faaf7e2)
5+
6+
7+
This Python script leverages OpenCV and a pre-trained SSD MobileNet v3 deep learning model for real-time object detection in video streams. It utilizes the COCO (Common Objects in Context) dataset for object class recognition.
8+
9+
## Requirements
10+
11+
- Python 3.x ([Download](https://www.python.org/downloads/))
12+
- OpenCV library: Install via `pip install opencv-python`
13+
- NumPy library: Install via `pip install numpy` (usually included with OpenCV)
14+
- `coco.names` file: Contains class labels for the COCO dataset (download from a reliable source)
15+
- `ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt` file: Model configuration file (download from a reliable source)
16+
- `frozen_inference_graph.pb` file: Model weights file (download from a reliable source)
17+
18+
## Instructions
19+
20+
1. **Download necessary files:**
21+
- Obtain the `coco.names`, `ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt`, and `frozen_inference_graph.pb` files from a trusted source (ensure compatibility with your OpenCV version). Place them in the same directory as your Python script.
22+
23+
2. **Run the script:**
24+
- Open a terminal or command prompt, navigate to the directory containing your script and files.
25+
- Execute the script using:
26+
```sh
27+
python object_detection.py
28+
```
29+
30+
## Code Breakdown
31+
32+
1. **Imports:**
33+
- `cv2`: Imports the OpenCV library for computer vision tasks.
34+
35+
2. **Threshold and Video Capture:**
36+
- `thres`: Sets the confidence threshold for object detection (adjust as needed).
37+
- `cap`: Initializes a video capture object (`VideoCapture(1)`) to access your webcam (or a different video source by providing its index or path).
38+
- `cap.set()`: Sets video capture properties:
39+
- `3`: Width (adjust for desired resolution)
40+
- `4`: Height (adjust for desired resolution)
41+
- `10`: Brightness (adjust for lighting conditions)
42+
43+
3. **Load Class Names:**
44+
- `classNames`: Creates an empty list to store object class names.
45+
- `classFile`: Path to the `coco.names` file.
46+
- Loads class names from the file and splits them into a list.
47+
48+
4. **Load Model Configuration and Weights:**
49+
- `configPath`: Path to the `ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt` file.
50+
- `weightsPath`: Path to the `frozen_inference_graph.pb` file.
51+
- `net`: Creates a detection model object using `cv2.dnn_DetectionModel()`.
52+
- Sets model input size, scale, mean, and color channel swapping parameters for compatibility with the model.
53+
54+
5. **Main Loop:**
55+
- `while True`: Continuously captures frames from the video stream.
56+
- `success, img`: Reads a frame and checks for success. Exits if unsuccessful.
57+
- `classIds`, `confs`, `bbox`: Performs object detection using the model on the current frame.
58+
- `classIds`: List of detected object class IDs.
59+
- `confs`: List of corresponding confidence scores (0-1).
60+
- `bbox`: List of bounding boxes (coordinates) for detected objects.
61+
- Prints detected object IDs and bounding boxes for debugging (optional).
62+
63+
6. **Draw Bounding Boxes and Labels:**
64+
- Checks if any objects were detected (`len(classIds) != 0`).
65+
- Iterates through detected objects using `zip`:
66+
- `box`: Current bounding box coordinates.
67+
- `classId`: Current object class ID (minus 1 for indexing).
68+
- `confidence`: Current object confidence score.
69+
- Draws a green rectangle around the detected object using `cv2.rectangle()`.
70+
- Displays the corresponding class name (uppercase) and confidence score (rounded to two decimal places) using `cv2.putText()`.
71+
72+
7. **Display and Exit:**
73+
- `cv2.imshow()`: Displays the processed frame with bounding boxes and labels in a window titled "Output".
74+
- `cv2.waitKey(1)`: Waits for a key press for 1 millisecond.
75+
- Exits the loop and releases resources if the 'q' key is pressed.
76+
77+
8. **Cleanup:**
78+
- Releases the video capture object and closes all OpenCV windows.
79+
```python
80+
cap.release()
81+
cv2.destroyAllWindows()
82+
```
83+
84+
## License
85+
86+
This project is licensed under the MIT License.
87+
thannk
88+
89+

OpenCV Projects/coco.names

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
person
2+
bicycle
3+
car
4+
motorcycle
5+
airplane
6+
bus
7+
train
8+
truck
9+
boat
10+
traffic light
11+
fire hydrant
12+
street sign
13+
stop sign
14+
parking meter
15+
bench
16+
bird
17+
cat
18+
dog
19+
horse
20+
sheep
21+
cow
22+
elephant
23+
bear
24+
zebra
25+
giraffe
26+
hat
27+
backpack
28+
umbrella
29+
shoe
30+
eye glasses
31+
handbag
32+
tie
33+
suitcase
34+
frisbee
35+
skis
36+
snowboard
37+
sports ball
38+
kite
39+
baseball bat
40+
baseball glove
41+
skateboard
42+
surfboard
43+
tennis racket
44+
bottle
45+
plate
46+
wine glass
47+
cup
48+
fork
49+
knife
50+
spoon
51+
bowl
52+
banana
53+
apple
54+
sandwich
55+
orange
56+
broccoli
57+
carrot
58+
hot dog
59+
pizza
60+
donut
61+
cake
62+
chair
63+
couch
64+
potted plant
65+
bed
66+
mirror
67+
dining table
68+
window
69+
desk
70+
toilet
71+
door
72+
tv
73+
laptop
74+
mouse
75+
remote
76+
keyboard
77+
cell phone
78+
microwave
79+
oven
80+
toaster
81+
sink
82+
refrigerator
83+
blender
84+
book
85+
clock
86+
vase
87+
scissors
88+
teddy bear
89+
hair drier
90+
toothbrush
91+
hair brush

OpenCV Projects/code.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import cv2
2+
3+
# Threshold to detect object
4+
thres = 0.45
5+
6+
# Initialize the video capture
7+
cap = cv2.VideoCapture(0)
8+
cap.set(3, 1280) # Set the width
9+
cap.set(4, 720) # Set the height
10+
cap.set(10, 70) # Set the brightness
11+
12+
# Load class names from the coco.names file
13+
classNames = []
14+
classFile = 'coco.names'
15+
with open(classFile, 'rt') as f:
16+
classNames = f.read().rstrip('\n').split('\n')
17+
18+
# Load model configuration and weights
19+
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
20+
weightsPath = 'frozen_inference_graph.pb'
21+
22+
# Create a detection model
23+
net = cv2.dnn_DetectionModel(weightsPath, configPath)
24+
net.setInputSize(320, 320)
25+
net.setInputScale(1.0 / 127.5)
26+
net.setInputMean((127.5, 127.5, 127.5))
27+
net.setInputSwapRB(True)
28+
29+
while True:
30+
success, img = cap.read()
31+
if not success:
32+
break
33+
34+
classIds, confs, bbox = net.detect(img, confThreshold=thres)
35+
print(classIds, bbox)
36+
37+
if len(classIds) != 0:
38+
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
39+
cv2.rectangle(img, box, color=(0, 255, 0), thickness=2)
40+
cv2.putText(img, classNames[classId - 1].upper(), (box[0] + 10, box[1] + 30),
41+
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
42+
cv2.putText(img, str(round(confidence * 100, 2)), (box[0] + 200, box[1] + 30),
43+
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
44+
45+
cv2.imshow("Output", img)
46+
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit
47+
break
48+
49+
cap.release()
50+
cv2.destroyAllWindows()
12.8 MB
Binary file not shown.

0 commit comments

Comments
 (0)