[enh] Adds an opencv realtime video capture + point detection
This commit is contained in:
parent
a9268b0d73
commit
d0cef35096
47
clitools/generators/opencv2.py
Executable file
47
clitools/generators/opencv2.py
Executable file
@ -0,0 +1,47 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
from numpy import asarray
|
||||
import potrace
|
||||
color = 65280
|
||||
camera = cv2.VideoCapture(0)
|
||||
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
|
||||
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
|
||||
camera.set(cv2.CAP_PROP_FPS, 5)
|
||||
print("camera FPS:{} height:{} width:{}".format(camera.get(cv2.CAP_PROP_FPS), camera.get(cv2.CAP_PROP_FRAME_WIDTH), camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
||||
while(camera.isOpened()):
|
||||
pl = []
|
||||
ret, frame = camera.read()
|
||||
if ret==True:
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
cv2.Canny(gray, 100,120 )
|
||||
(thresh, gray) = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
|
||||
nb = cv2.cvtColor(asarray(gray), cv2.COLOR_RGB2BGR)
|
||||
image = Image.fromarray(nb).convert("1")
|
||||
image.save( "/tmp/cv.png")
|
||||
bmp = potrace.Bitmap( asarray( image ) )
|
||||
# Trace the bitmap to a path
|
||||
path= bmp.trace(turdsize=16,alphamax=0.0, opticurve=0, opttolerance=1.0)
|
||||
# Iterate over path curves
|
||||
for curve in path:
|
||||
start = curve.start_point
|
||||
pl.append([start[0],start[1],0])
|
||||
pl.append([start[0],start[1],color])
|
||||
for segment in curve:
|
||||
end_point_x, end_point_y = segment.end_point
|
||||
if segment.is_corner:
|
||||
#c_x, c_y = segment.c
|
||||
pass
|
||||
else:
|
||||
c1_x, c1_y = segment.c1
|
||||
x, y = segment.c2
|
||||
pl.append([x,y,color])
|
||||
pl.append([start[0],start[1],0])
|
||||
ret, frame = camera.read()
|
||||
#print(len(pl), flush = True)
|
||||
print(pl, flush = True)
|
||||
else:
|
||||
break
|
||||
|
||||
# Release everything if job is finished
|
||||
camera.release()
|
Loading…
Reference in New Issue
Block a user