Re: OpenCV hover-assist
Posted: Thu Jan 25, 2018 12:18 pm
After some more experimenting and understanding the differences between older and newer versions of the various libraries, here is some example code that takes a picture from a webcam (you could presumably have more than one as video0, video1 etc), and then scans this for "blobs", and prints out the coordinates of the blobs.
So this could be used a a simple tracker for CrazyFlie if it has a suitably high-contrast "blob" stuck to it. Since this runs in Python 3, you could now add mc.commander code etc to control the CrazyFlie from the same script and run it from IDLE 3, etc.
I am just putting this code snippet here for the record to show the libraries and syntax needed for some of the commands. For instance the pillow (PIL) library is needed, even though, as far as I can see, no PIL commands are used... I don't understand this either! I stumbled upon this when trying to use PIL to display an image captured by cv2, just to prove that cv2 was even working.
So, you will need to do:
pip3 install pillow
pip3 install opencv-python
pip3 install pygame
first.
# Standard imports
import cv2
from PIL import Image
import numpy as np
import pygame
import pygame.camera
import time
pygame.camera.init()
pygame.camera.list_cameras()
cam = pygame.camera.Camera("/dev/video0", (640, 480))
cam.start()
time.sleep(0.1) # You might need something higher in the beginning
img = cam.get_image()
pygame.image.save(img, "pygamex.jpg")
cam.stop()
# imports the image you just captured
im = cv2.imread('pygamex.jpg',cv2.IMREAD_GRAYSCALE)
print (im)
# writes a copy of the image if you want to keep track of things
cv2.imwrite('messigray.png',im)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10;
params.maxThreshold = 200;
# Filter by Area.
params.filterByArea = True
params.minArea = 1500
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)
cv2.imwrite("with keypoints.jpg", im_with_keypoints)
# displays coordinates of keypoints
kl=len(keypoints)
print (kl)
x=0
while x<kl:
kl1=0
kl1=(keypoints[x].pt)
klx=kl1[0]
kly=kl1[1]
print (keypoints[x].pt)
print (klx)
print (kly)
x+=1
print ("Finished")
cv2.waitKey(0)
So this could be used a a simple tracker for CrazyFlie if it has a suitably high-contrast "blob" stuck to it. Since this runs in Python 3, you could now add mc.commander code etc to control the CrazyFlie from the same script and run it from IDLE 3, etc.
I am just putting this code snippet here for the record to show the libraries and syntax needed for some of the commands. For instance the pillow (PIL) library is needed, even though, as far as I can see, no PIL commands are used... I don't understand this either! I stumbled upon this when trying to use PIL to display an image captured by cv2, just to prove that cv2 was even working.
So, you will need to do:
pip3 install pillow
pip3 install opencv-python
pip3 install pygame
first.
# Standard imports
import cv2
from PIL import Image
import numpy as np
import pygame
import pygame.camera
import time
pygame.camera.init()
pygame.camera.list_cameras()
cam = pygame.camera.Camera("/dev/video0", (640, 480))
cam.start()
time.sleep(0.1) # You might need something higher in the beginning
img = cam.get_image()
pygame.image.save(img, "pygamex.jpg")
cam.stop()
# imports the image you just captured
im = cv2.imread('pygamex.jpg',cv2.IMREAD_GRAYSCALE)
print (im)
# writes a copy of the image if you want to keep track of things
cv2.imwrite('messigray.png',im)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10;
params.maxThreshold = 200;
# Filter by Area.
params.filterByArea = True
params.minArea = 1500
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)
cv2.imwrite("with keypoints.jpg", im_with_keypoints)
# displays coordinates of keypoints
kl=len(keypoints)
print (kl)
x=0
while x<kl:
kl1=0
kl1=(keypoints[x].pt)
klx=kl1[0]
kly=kl1[1]
print (keypoints[x].pt)
print (klx)
print (kly)
x+=1
print ("Finished")
cv2.waitKey(0)