-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall-object_detection.py
More file actions
35 lines (34 loc) · 1.26 KB
/
all-object_detection.py
File metadata and controls
35 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import cv2
import time
import numpy as np
from google.colab.patches import cv2_imshow
confidence = 0.6
Nms= 0.3
class_names = []
with open("coco.names", "r") as f:
class_names = [cname.strip() for cname in f.readlines()]
img = cv2.imread("img2.jpg.webp")
arc = cv2.dnn.readNet("yolov4.weights","yolov4.cfg")
arc.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
arc.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16)
model = cv2.dnn_DetectionModel(arc)
model.setInputParams(size=(640,640), scale=1/255, swapRB=True)
x =time.time()
classes, scores, boxes = model.detect(img, confidence, Nms)
y= time.time()
fps=1/(y-x)
for (classid, score, box) in zip(classes, scores, boxes):
if isinstance(classid, (list, np.ndarray)):
classid_value = classid[0]
else:
classid_value = classid
# Check if score is a list or a scalar
if isinstance(score, (list, np.ndarray)):
score_value = score[0]
else:
score_value = score
label = "%s : %.2f" % (class_names[classid_value], score_value)
cv2.rectangle(img,box,color=(255, 200, 10),thickness=2)
cv2.putText(img, label, (box[0],box[1]-10),cv2.FONT_HERSHEY_SIMPLEX,0.6,(25,55,255),2)
cv2.putText(img, "FPS:{0:.2f}".format(fps),(20, 25), cv2.FONT_HERSHEY_PLAIN,fontScale=2,color=(255, 0, 0),thickness=2)
cv2_imshow(img)