|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么照片无法拍摄成功
#! /usr/bin/env python
# -*- encoding: UTF-8 -*-
import sys
sys.path.append("D:\pynaoqi-python2.7-2.8.6.23-win64-vs2015-20191127_152649\pynaoqi-python2.7-2.8.6.23-win64-vs2015-20191127_152649\lib")
import qi
import time
import math
import cv2
import numpy as np
import threading
import vision_definitions
import random
session = qi.Session()
try:
session.connect("tcp://169.254.137.173:9559")
except RuntimeError:
print("无法连接到NAOqi at ip <NAO_IP> on port <NAO_PORT>.")
exit(1)
video = session.service("ALVideoDevice")
motion=session.service("ALMotion")
posture=session.service("ALRobotPosture")
rad=0
def f0(img):
if(img):
image =np.frombuffer(img[6], dtype=np.uint8)
image = image.reshape(img[1], img[0], img[2])
graying=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
cv2.imwrite(r"D:/a.jpg",graying)
#graying=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#lowerGreen = np.array( [0, 0, 150])
#upperGreen = np.array([120, 17, 255])
#mask =cv2.inRange(image,lowerGreen, upperGreen)
#greenThings=cv2.bitwise_and(image, image,mask=mask)
img = cv2.GaussianBlur(cv2,(3,3),0)
imcan =cv2.Canny(img,50,150,apertureSize=3)
cv2.imwrite(r"D:/a1.jpg",imcan)
lines =cv2.HoughLinesP(imcan,1,np.pi/180,50,minLineLength=50, maxLineGap=10)
return lines
def f1(lines):
k1=0
k2=0
if(lines and len(lines)>=2):
for line in lines:
x1,y1,x2,y2 = line[0]
k=1.0*(y2-y1)/(x2-x1)
if(k>k1):
k1=k
if(k<k2):
k2=k
return k1
def f3():
motion.wakeUp()
posture.goToPosture("StandInit",0.5)
while(1):
#motion.moveToward(0.5,0,rad)
time.sleep(1)
pass
# NAOqi Session初始化
def subscribe_camera(v):
v.setActiveCamera(1)
v1 = v.subscribe("python_nao"+str(random.randint(1, 100000)), vision_definitions.kQVGA, vision_definitions.kBGRColorSpace, 30)
v.setCameraParameter(v1, 22, 2)
return v1
#获得图片,因为可能偶尔出现硬件错误所以使用 try 语句进行操作。
def get_image(v,v1):
while(1):
try:
naoImage = v.getImageRemote(v1)
img =np.frombuffer(naoImage[6], dtype=np.uint8)
img = img.reshape(naoImage[1], naoImage[0], naoImage[2])
img=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
break
except TypeError as ValueError:
continue
return img
def detect_lines(image):
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
low_range = np.array([0, 100, 0])
high_range = np.array([180, 255, 255])
hsv_range = cv2.inRange(hsv_image, low_range, high_range)
kernel = np.ones((4, 5), np.uint8)
closed = cv2.morphologyEx(hsv_range, cv2.MORPH_CLOSE, kernel)
minLineLength = 20
maxLineGap = 5
lines = cv2.HoughLinesP(closed, 1, np.pi/180.0, minLineLength, maxLineGap)
front_line = (0, 0, 0, 0, 0.0)
left_line = (0, 0, 0, 0, 0.0)
right_line = (0, 0, 0, 0, 0.0)
cnt = 0
def main():
#thr1=threading.Thread(target=f3)
#thr0.start()
#thr1.start()
#video.unsubscribe(video1)
video1=subscribe_camera(video)
cv2.imwrite(r"D:/aaa/a1.jpg",get_image(video,video1))
motion.wakeUp()
posture.goToPosture("StandInit",0.5)
motion.moveToward(0.,0,0)
pass
main()
|
|