I've been using the image AI library to create a simple object detection system using python, which currently records video using the webcam and as far as I can tell detects objects live, once the program stops, a video file is created with object markers overlayed on the video. The working code:
from imageai.Detection import VideoObjectDetection
import os
import cv2
camera = cv2.VideoCapture(0)
current_directory = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(r"C:\Users\Luca\Desktop\image\yolo.h5")
detector.loadModel()
detections = detector.detectObjectsFromVideo(
camera_input=camera,
output_file_path = r"C:\Users\Luca\Desktop\image\footage_detected", frames_per_second=20, log_progress=True)
for eachObject in detections:
print(
eachObject["name"] , " : ",
eachObject["percentage_probability"], " : ",
eachObject["box_points"] )
print("--------------------------------")
vid.release()
cv2.destroyAllWindows()
I would like the program to display each frame on the display as it is being recorded with the object markers overlayed on the live feed as it detects them. I am only able to get the live camera feed directly from opencv without the object markers.
I attempted to use opencv and imageai to take each frame, use the detectObjectsFromImage
method to process each frame and then using the output of the method with the opencv imshow
method. However it gives the error:
ValueError: "Ensure you specified correct input image, input type, output type and/or output image path"
Here is the code where I attempt to do this:
from imageai.Detection import ObjectDetection
import os
import cv2
current_directory = os.getcwd()
camera = cv2.VideoCapture(0)
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(r"C:\Users\Luca\Desktop\image\yolo.h5")
detector.loadModel()
while True:
ret, frame = camera.read()
returned_image, detections = detector.detectObjectsFromImage(
input_image = (frame),
output_type = "array"
)
for eachObject in detections:
print(
eachObject["name"] , " : ",
eachObject["percentage_probability"], " : ",
eachObject["box_points"] )
print("--------------------------------")
cv2.imshow('frame', returned_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Please let if this is possible, and if so how can it be done using the ImageAI library? Thank you in advance.
Edit: here is the complete traceback.
Traceback (most recent call last):
File "C:\Users\Luca\anaconda3\envs\objectreconition\lib\site-packages\imageai\Detection\__init__.py", line 285, in detectObjectsFromImage
input_image = cv2.imread(input_image)
TypeError: Can't convert object to 'str' for 'filename'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Luca\Desktop\image\main 2.py", line 22, in <module>
output_type = "array"
File "C:\Users\Luca\anaconda3\envs\objectreconition\lib\site-packages\imageai\Detection\__init__.py", line 392, in detectObjectsFromImage
"Ensure you specified correct input image, input type, output type and/or output image path ")
ValueError: Ensure you specified correct input image, input type, output type and/or output image path