1 year ago

#379162

test-img

Сергей Степин

Calculate the number of objects in a video using the imageai library

Hello everyone I want to count the number of objects in a video using the imageai library.
This is my source code:

from imageai.Detection import ObjectDetection
import cv2
import time

video = cv2.VideoCapture('model_path\Author\ducks.mp4')

detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath('model_path\Author\yolo.h5')
detector.loadModel()

finish = 0
array_detection = []
counts = 0

while video.isOpened():
    ret, frame = video.read()
    start = time.time()
    if start - finish > 0.5:
        _, array_detection = detector.detectObjectsFromImage(
            input_image=frame, input_type="array", output_type="array")
        finish = time.time()
        print(array_detection)

    for obj in array_detection:
        coord = obj['box_points']
        cv2.rectangle(frame, (coord[0], coord[1],
                              coord[2], coord[3]), (0, 0, 255))
        cv2.putText(frame, obj['name'], (coord[0], coord[1]),
                    cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1)

    cv2.imshow('Test', frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break


cv2.destroyAllWindows()

How I can do this?

I tried to search for information, but unfortunately I didn't find anything.

python

opencv

object-detection

imageai

0 Answers

Your Answer

Accepted video resources