|
| 1 | +import cv2 as cv |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +confThreshold = 0.5 |
| 5 | +nmsThreshold = 0.5 |
| 6 | + |
| 7 | +def getOutputsNames(net): |
| 8 | + layersNames = net.getLayerNames() |
| 9 | + return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()] |
| 10 | + |
| 11 | +def postprocess(frame, outs): |
| 12 | + frameHeight = frame.shape[0] |
| 13 | + frameWidth = frame.shape[1] |
| 14 | + classIds = [] |
| 15 | + confidences = [] |
| 16 | + boxes = [] |
| 17 | + |
| 18 | + for out in outs: |
| 19 | + for detection in out: |
| 20 | + scores = detection[5:] |
| 21 | + classId = np.argmax(scores) |
| 22 | + confidence = scores[classId] |
| 23 | + if confidence > confThreshold: |
| 24 | + center_x = int(detection[0] * frameWidth) |
| 25 | + center_y = int(detection[1] * frameHeight) |
| 26 | + width = int(detection[2] * frameWidth) |
| 27 | + height = int(detection[3] * frameHeight) |
| 28 | + left = int(center_x - width / 2) |
| 29 | + top = int(center_y - height / 2) |
| 30 | + classIds.append(classId) |
| 31 | + confidences.append(float(confidence)) |
| 32 | + boxes.append([left, top, width, height]) |
| 33 | + |
| 34 | + indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold) |
| 35 | + for i in indices: |
| 36 | + i = i[0] |
| 37 | + box = boxes[i] |
| 38 | + left = box[0] |
| 39 | + top = box[1] |
| 40 | + width = box[2] |
| 41 | + height = box[3] |
| 42 | + color = (255,255,0) |
| 43 | + if classIds[i] == 0: # Blue if person |
| 44 | + color = (255,0,0) |
| 45 | + elif classIds[i] == 2: # Green if car |
| 46 | + color = (0,255,0) |
| 47 | + elif classIds[i] == 7: # Red if Truck |
| 48 | + color = (0,0,255) |
| 49 | + |
| 50 | + label = '%s:%s' % (classes[classIds[i]],round(confidences[i]*100)) |
| 51 | + cv.putText(frame,label,(left,top-10),cv.FONT_HERSHEY_SIMPLEX,0.3,color,1) |
| 52 | + cv.rectangle(frame, (left, top), (left+width, top+height),color, 2) |
| 53 | + |
| 54 | +classes = [] |
| 55 | +with open(r"path\\coco.names") as f: |
| 56 | + classes = f.read().rstrip('\n').split('\n') |
| 57 | +print(classes) |
| 58 | + |
| 59 | + |
| 60 | +modelConfiguration = "path\\yolov3.cfg" |
| 61 | +weights = "path\\yolov3.weights" |
| 62 | +net = cv.dnn.readNetFromDarknet(modelConfiguration,weights) |
| 63 | +net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV) |
| 64 | +net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU) |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +cap = cv.VideoCapture("demo_Trim.mp4") |
| 69 | +while cap.isOpened(): |
| 70 | + ret, frame = cap.read() |
| 71 | + if not ret: |
| 72 | + print("Can't receive frame (stream end?). Exiting ...") |
| 73 | + break |
| 74 | + blob = cv.dnn.blobFromImage(frame,1/225,(416,416),[0,0,0],1) |
| 75 | + net.setInput(blob) |
| 76 | + outs = net.forward(getOutputsNames(net)) |
| 77 | + postprocess(frame, outs) |
| 78 | + |
| 79 | + cv.imshow("frame",frame) |
| 80 | + if cv.waitKey(100) == 27: |
| 81 | + break |
| 82 | +cap.release() |
| 83 | +cv.destroyAllWindows() |
| 84 | + |
0 commit comments