pythonstringopencvar.drone

Getting An Image from The ArDrone 2.0, And Using cv2


I am trying to obtain images from the ArDrone 2.0 and using Python OpenCV, cv2, to process images and create a controller.

This is my code:

import cv2
import numpy as np
from pydrone import libardrone

drone = libardrone.ARDrone()
cap = drone.image

while(True):
    ret, frame = cap
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

However, I keep getting this problem:

Traceback (most recent call last):
  File "ArDrn/7/drony.py", line 14, in <module>
    ret, frame = cap
TypeError: 'NoneType' object is not iterable

Can someone tell me what am I doing wrong, please?


Solution

  • It seems to me that you might need to change the logic slightly to check if cap is None before trying to use it:

    while(True):
        cap = drone.image
        if not cap:
            time.sleep(0.1)   # or something to save just a touch of CPU spin time, optional
            continue
        ret, frame = cap
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('frame',gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    

    It may be that while it is in the middle of switching images or some time like that, there may be a period of time when cap is None. Additionally, it seems you were not updating cap to point to a new image each loop, so it's possible that could be an issue too.