私たちは IT の学生で、NAO に取り組んでいます (つまり、私たちは非常に初心者です)。NAOカメラによる地上でのライン検出による経路探索を実装したいと考えています。その目的のために Choregraphe でPython Script Boxを作成しましたが、「 ALVideoDevice」を正しく購読および購読解除できません。
Python Script Box のコードは次のとおりです。
import sys
import numpy as np
import cv2
from naoqi import ALProxy
import PIL
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import vision_definitions as vd
def avg_lines_angles(lines):
angle_sum = 0
for line in lines:
angle_sum = angle_sum + get_angle(line[0])
return angle_sum/len(lines)
def get_angle(tupl):
x1,y1,x2,y2 = tupl
dy = (y2-y1)
dx = (x2-x1)
angle = np.arctan2(dy,dx)
if angle < 0:
angle = angle + np.pi
return angle
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
self.nameID = "test_subscribe"
self.videoDevice = ALProxy('ALVideoDevice')
self.captureDevice = self.videoDevice.subscribe(self.nameID, vd.k4VGA, vd.kRGBColorSpace, 20)
def onUnload(self):
#put clean-up code here
self.videoDevice.unsubscribe(self.nameID)
def onInput_onStart(self):
index = None
# get image
result = self.videoDevice.getImageRemote(self.captureDevice);
if result == None:
print 'cannot capture.'
#self.onUnload()
self.onError()
# show image
else:
basewidth = 600
img = Image.fromstring("RGB",(result[0],result[1]),result[6])
self.videoDevice.releaseImage(self.nameID)
img = img.crop((img.size[0]/4,3*img.size[1]/5,3*img.size[0]/4,img.size[1]))
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
gray = img.convert("L")
gray = np.array(gray)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite("/home/nao/Pictures/debug.jpg",edges)
lines = cv2.HoughLinesP(edges,1,np.pi/180,60,minLineLength=50,maxLineGap=30)
try:
angle = avg_lines_angles(lines)
self.onStopped(angle)
except:
self.onError()
#self.onUnload()
def onInput_onStop(self):
#self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped(-1) #activate the output of the box
クラスのロード時にサービスにサブスクライブし、クラスのアンロード時にサブスクライブを解除します。本当に奇妙なのは、購読解除時のエラーです:
[ERROR] vision.videodevice :unsubscribe:0 Can't unsubscribe "test_subscribe", subscriber is unknown.
私たちのボックスは、購読者の数が私たちができることを制限するループに陥っています。ALVideoDevice から画像を取得できるので、サブスクライブします。ただし、登録解除に同じ名前を使用してもまったく機能しません。Python SDK API には何も見つかりませんでした。何を行っているかを正確に説明するいくつかのチュートリアルだけです (購読 -> ALVideoDevice を使用するコード -> 購読解除)