Skip to content

Instantly share code, notes, and snippets.

@yushulx
Created November 20, 2020 09:24
Show Gist options
  • Save yushulx/ffa82a3987ab82f81311f9307e76c9de to your computer and use it in GitHub Desktop.
Save yushulx/ffa82a3987ab82f81311f9307e76c9de to your computer and use it in GitHub Desktop.
def postprocess(frame, outs):
frameHeight, frameWidth = frame.shape[:2]
classIds = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
classId = np.argmax(scores)
confidence = scores[classId]
if confidence > threshold:
x, y, width, height = detection[:4] * np.array([frameWidth, frameHeight, frameWidth, frameHeight])
left = int(x - width / 2)
top = int(y - height / 2)
classIds.append(classId)
confidences.append(float(confidence))
boxes.append([left, top, int(width), int(height)])
indices = cv.dnn.NMSBoxes(boxes, confidences, threshold, threshold - 0.1)
for i in indices:
i = i[0]
box = boxes[i]
left = box[0]
top = box[1]
width = box[2]
height = box[3]
# Draw bounding box for objects
cv.rectangle(frame, (left, top), (left + width, top + height), (0, 0, 255), thickness)
# Draw class name and confidence
label = '%s:%.2f' % (classes[classIds[i]], confidences[i])
cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment