我正在使用opencv在图像中进行颜色检测的小项目。我偶然发现了一个名为 gTTS 的概念,将文本转换为语音。我试过了,无法正确实施。
#将文本转换为语音的代码
speech = gTTS(text=get_color_name(r, g, b), lang='en', slow=False)
speech.save('vc.mp3')
os.system('start vc.mp3')
这里,get_color_name()函数返回颜色的名称。
#环形
while True:
cv2.imshow('image', img)
if clicked:
cv2.rectangle(img, (20, 20), (600, 60), (b, g, r), -1)
text1 = get_color_name(r, g, b) + ' R=' + str(r) + ' G=' + str(g) + ' B=' + str(b)
cv2.putText(img, text1, (50, 50), 2, 0.8, (255, 255, 255), 2, cv2.LINE_AA)
if r+g+b >= 500:
cv2.putText(img, text1, (50, 50), 2, 0.8,
(0, 0, 0), 2, cv2.LINE_AA)
speech()
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
注意:单击是当用户单击图像时,它变为True
当我双击图像时,我需要声音告诉我颜色名称一次。但相反,它在循环中运行。如果我只需要声音告诉我颜色的名称一次,应该怎么做?
谢谢您的帮助 :)