0

I am able to process the video frames by saing the frame as an image and then processing it. But was unable to pass frame directly to the object detection. Saving image with imwrite is making program slow...

Here is my main method:

cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=2), cv2.CAP_GSTREAMER)

if cap.isOpened():
    window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
    # Window
    while cv2.getWindowProperty("CSI Camera", 0) >= 0:
        ret_val, frame = cap.read()
        if not ret_val:
            break
        frame = imutils.resize(frame, width=600)

        #cv2.imwrite('box.jpg', frame)
        #image = Image.open(path)
        #Error in here!!!
        predictions = od_model.predict_image(frame)


        for x in range(len(predictions)):
            probab = (predictions[x]['probability'])*100
            if(probab > 45):
                print(predictions[x]['tagName'], end=' ')
                print(probab)
        #cv2.imshow("CSI Camera", frame)
        # This also acts as
        keyCode = cv2.waitKey(30) & 0xFF
        # Stop the program on the ESC key
        if keyCode == 27:
            break
    cap.release()
    cv2.destroyAllWindows()
else:
    print("Unable to open camera")

Error Message:

predictions = od_model.predict_image(frame)
File "/home/bharat/New_IT3/object_detection.py", line 125, in 
predict_image
inputs = self.preprocess(image)
File "/home/bharat/New_IT3/object_detection.py", line 130, in 
preprocess
image = image.convert("RGB") if image.mode != "RGB" else image
AttributeError: 'numpy.ndarray' object has no attribute 'mode'
vasu
  • 25
  • 7
  • if you uncomment #cv2.imwrite('box.jpg', frame) #image = Image.open(path) then?? – spaceman Feb 20 '20 at 18:25
  • Then it is working but writing image to memory again and again is slowing down the process. I am looking for passing object directly – vasu Feb 20 '20 at 18:29
  • 1
    from PIL import Image im = Image.fromarray(frame) //import PIL package – spaceman Feb 20 '20 at 18:31

1 Answers1

0

open cv reads image in bgr colour spectrum conver it to rgb and send the image for detection, api for the same is -

frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

Suman
  • 354
  • 3
  • 10