Mycroft + camera
文章目录
Mycroft是一款语音助手,官方文档 https://mycroft.ai/
问题现象
Mycroft执行拍照功能时发生的问题:
如果直接在intent里调用拍照函数,第二次无法打开
但是函数本身是没有问题的,单个文件运行拍照函数,可以重复运行
import cv2
import time
def take_photo():
print('take photo process start')
cap = cv2.VideoCapture(0)
img_name = 'cap_img_' + str(time.time()) + '.jpg'
# Change this variable to the path you want to store the image
img_path = 'xxx/' + img_name
#<-- Take photo in specific time duration -->
cout = 0
while True:
ret, frame = cap.read()
cv2.waitKey(1)
cv2.imshow('capture', frame)
cout += 1
if cout == 50:
cv2.imwrite(img_path, frame)
break
cap.release()
cv2.destroyAllWindows()
print('take photo process end')
take_photo()如何解决
参考这个skill https://github.com/eClarity/s...
在调用拍照功能时,用了进程池。
所以怀疑,单个文件调用时,因为程序退出,照相机正常关闭;
但在Mycroft的intent里调用,cap.release() 可能因为什么原因,没有正常关闭照相机,所以用一个进程单独调用,然后进程关闭,照相机就可以正常关闭。
@intent_handler('example.intent')
def handle_take_photo(self, message):
self.speak_dialog('take.photo')
self.img_multi = ''
self.img_hand = ''
img_queue = Queue()
take_photo_process = Process(target=take_photo, args=(img_queue,))
take_photo_process.daemon = True
take_photo_process.start()
take_photo_process.join()
self.img_multi = img_queue.get()
self.speak('something')这里遇到了一个新问题,本来想直接传输nparray,也就是image,但是不知道为什么不能获取到,img_queue.get() 获取不到,猜测是太大了。后来换成传输image的保存路径。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论