Hello there, guys.
Back with another issue. This time to do with OpenCV. So I have an object detection project written with the guidance of a course which can detect objects and indicate what they are (and how accurate they are). However, instead of the program constantly displaying video(as it did in the course), it just sends a pop-up of whatever image it finds and in order for the program to continue you have to close the screenshot before it gives you a new one. Essentially, it just screenshots the thing and gives the user the screenshot instead of showing you in real-time. I have never used OpenCV before and therefore have no idea how to do this.
Here is the code I have written so far:
import cv2
thres = 0.5
# img = cv2.imread('lena.png')
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
classNames = []
classFile = 'coco.names'
with open(classFile, 'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
config_path = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weights_path = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weights_path, config_path)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
while True:
success, img = cap.read()
class_ids, confs, bbox = net.detect(img, confThreshold=thres)
print(class_ids, bbox)
if len(class_ids) != 0:
for class_id, confidence, box in zip(class_ids.flatten(), confs.flatten(), bbox):
cv2.rectangle(img, box, color=(0, 0, 255), thickness=3)
cv2.putText(img, classNames[class_id - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
cv2.putText(img, str(round(confidence * 100, 2)), (box[0] + 200, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("Output", img)
cv2.waitKey(0)
If anyone could help me on this, it would be greatly appreciated!