照片拍摄失败
为什么照片无法拍摄成功#! /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, dtype=np.uint8)
image = image.reshape(img, img, img)
graying=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
cv2.imwrite(r"D:/a.jpg",graying)
#graying=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#lowerGreen = np.array( )
#upperGreen = np.array()
#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
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, dtype=np.uint8)
img = img.reshape(naoImage, naoImage, naoImage)
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()
high_range = np.array()
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()
在你的代码中,我看到了一些问题。首先,你在创建图像时使用了错误的参数。应该根据图像的维度(宽度和高度)来创建图像,而不是使用单个值。所以你需要更改以下这行代码:
image = image.reshape(img, img, img)
改为:
image = image.reshape(img_height, img_width, img_channels)
另外,你在使用`cv2.GaussianBlur`函数时,第一个参数是错误的。需要传入原始图像,而不是`cv2`模块本身。所以你需要更改以下这行代码:
img = cv2.GaussianBlur(cv2,(3,3),0)
改为:
img = cv2.GaussianBlur(image,(3,3),0)
最后,你在计算直线斜率时可能出现了错误。你没有给出斜率的初始值,所以这可能导致了错误的结果。在循环之前,你需要初始化`k1`和`k2`变量,例如:
k1 = float('-inf')
k2 = float('inf')
这样可以确保在循环中找到比初始斜率更大或更小的值。
希望这些修改能帮助你解决照片拍摄失败的问题!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 {:10_256:}{:10_256:}
页:
[1]