系統環境: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()
問題截圖:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/16589.html
