def getFileFace(self): imgName, imgType = QFileDialog.getOpenFileName( self, "打开图片", "", "*.jpg;;*.png;;All Files(*)") if imgName == "": return else: imgName = imgName.replace('\\', '/') cv2Image = cv2.imread(imgName) img = cv2.cvtColor(cv2Image, cv2.COLOR_BGR2RGB) faceLocation = getFaceLocations(img) if len(faceLocation) == 0: QMessageBox.warning(self, '警告', '没有识别到人脸!', QMessageBox.Yes) self.pushButton_3.setEnabled(False) return else: self.faceEncode = getFaceEncode(img, faceLocation)[0] # 图片转换 shrink = cv2.resize(cv2Image, self.labelSize, interpolation=cv2.INTER_AREA) # 更改编码 shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB) image = QImage(shrink.data, shrink.shape[1], shrink.shape[0], shrink.shape[1] * 3, QImage.Format_RGB888) self.label.setPixmap(QPixmap().fromImage(image)) self.img = img self.pushButton_3.setEnabled(True)
def addPerson(image, person): # 获取数据库操作 p = person_dao_impl.PersonDaoImpl() # 获取人脸数组 image_numpy = face.imageFile2Numpy(image) # 获取人脸数组 encode = face.getFaceEncode(image_numpy) # 查找人脸是否存在 if face.findFaceInLib(encode) is True: # 如果存在 return False else: person.faceid = encode # 人脸图片转码 person.pic = image_numpy # 添加到数据库 p.addPerson(person) # 更新数据库信息到本地 p.updateGlFaceLib() return True
def run(self): print("FrameService类.GrayThread已打开") # 等待事件触发 try: while True: # 等待灰度图 self.channel.threadEvent[1].wait() # 先获得灰度图 gray = self.channel.frame[1] # 获得人脸位置 faceLocation = getFaceLocations(gray) # 获得人脸位置 if len(faceLocation) == 0: print("没有人脸") self.channel.threadEvent[1].clear() continue else: # 获得人脸编码 # .repeat(3, 2) faceEncoding = getFaceEncode(gray,faceLocation) print("人脸编码为:{0}".format(faceEncoding)) pass # 通知水印。。 self.channel.threadEvent[1].clear() except: error = traceback.fromat_exc() raise Exception(error)
def getFaceInCamera(self): # 打开人脸识别框 cap = cv2.VideoCapture(2) faceLocation = None img = None flag = False while (1): if cv2.waitKey(1) & 0xFF == ord('q'): break # get a frame ret, frame = cap.read() # 转码 img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) faceLocation = getFaceLocations(img) # show a frame if len(faceLocation) == 0: cv2.imshow("show", frame) continue else: # 设置图片 self.frame = frame # 遍历人脸位置信息 for top, right, bottom, left in faceLocation: # 对人脸画框 cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.imshow("show", frame) # 获取编码 self.faceEncode = getFaceEncode(img, faceLocation)[0] time.sleep(2) flag = True break cap.release() cv2.destroyAllWindows() # 是否获取 if flag == False: return False # 图片转换 shrink = cv2.resize(self.frame, self.labelSize, interpolation=cv2.INTER_AREA) # 更改编码 shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB) image = QImage(shrink.data, shrink.shape[1], shrink.shape[0], shrink.shape[1] * 3, QImage.Format_RGB888) self.label.setPixmap(QPixmap().fromImage(image)) self.img = img # 收获到人脸判断是否存在 # 获取人脸后退出 # 将人脸写到label # 打开输入选项 self.pushButton_3.setEnabled(True)