【求助】Python OpenCV库imshow函数出现多个窗口的问题

Andy Rice 2020-08-07 11:21:15
系统环境:Ubuntu

语言:Python

预期目的:利用face_recognition库与OpenCV库实现电脑摄像头的实时人脸识别

出现问题:实时人脸检测等功能没有问题,但是每一次打开程序时都会打开除了摄像头画面外的多个灰色小窗口,也就是说每次打开程序时,除了实时摄像头检测画面还会有多个灰色的,没有任何显示的窗口一起被打开。

源代码:
1 import cv2
2 import face_recognition
3 import numpy as np
4 import knn_clf as kc
5
6
7 #define functions
8 def train_classify_model(face_data_path,classifier_name):
9 try:
10 classify_file = open(classifier_name)
11 classify_file.close()
12 except FileNotFoundError:
13 print("Training classify model, please stand by.\n")
14 print("......\n")
15 classify_file = kc.train(face_data_path,classifier_name)
16 print("......\n")
17 print("Trainning Process sucessfully completed.\n")
18
19
20 #train classify model
21 classify_model_name = 'a.txt'
22 classify_model = "../train_model/" + classify_model_name
23 known_face_path = "../face_data_base/known_face"
24 train_classify_model(known_face_path,classify_model)
25
26
27 #initiate camera
28 v_c = cv2.VideoCapture(0)
29 #rval, frame = v_c.read()
30 while True:
31 rval, frame = v_c.read()
32 #convert color to RGB
33 resized_frame = cv2.resize(frame,None,fx=0.25,fy=0.25)
34 rgb_resized_frame = cv2.cvtColor(resized_frame,cv2.COLOR_BGR2RGB)
35 #compare faces
36 classify_results = kc.predict(rgb_resized_frame,None,classify_model,0.4)
37 if classify_results != []:
38 #draw a rectangle and name on faces
39 for classify_result in classify_results:
40 #if classify_result[0] == "unknown":
41 top,right,bottom,left = classify_result[1]
42 top *= 4
43 right *= 4
44 bottom *= 4
45 left *= 4
46 cv2.rectangle(frame,(left,top),(right,bottom),(145,158,0),2)
47 cv2.rectangle(frame,(left,bottom-35),(right,bottom),(255,255,255),-1)
48 cv2.putText(frame,classify_result[0],(left+6,bottom-6),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0) ,1)
49 cv2.imshow("Classifier",frame)
50 if cv2.waitKey(1) & 0xFF == ord('q'):
51 break
52
53 v_c.release()
54 cv2.destroyAllWindows()

问题截图:
...全文
320 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧