def tryFaceAPIError(faceapijson): if 'error' in faceapijson: print('CODE:', faceapijson['error']['code']) print('MESSAGE:', faceapijson['error']['message']) text = faceapijson['error']['code'] + ": " + faceapijson['error'][ 'message'] ClassCV.cv_ImageText('存取發生錯誤!', text)
def Identify(self, pictureurl): ''' 14: 進行「辨識」,使用 image URL or 檔案路徑 ''' start = int(round(time.time() * 1000)) print('開始計時 identify') faceApi = FaceAPI.Face(api_key, host) personApi = FaceAPI.Person(api_key, host) print('載入 class', int(round(time.time() * 1000) - start), 'ms') #imageurl = input('請輸入準備要辨識的 image URL or 檔案路徑:') if pictureurl.startswith('http'): detectfaces = faceApi.detectURLImages(pictureurl) else: pictureurl = pictureurl.strip() statinfo = os.stat(pictureurl) print('檔案大小:', statinfo.st_size, 'Bytes') if statinfo.st_size < 1024: print('圖檔太小 不可小於 1KB') sys.exit(1) elif statinfo.st_size > 4 * 1024 * 1024: print('圖檔太大 不可大於 4MB') im = Image.open(pictureurl) out = im.resize((128, 128)) im.save(pictureurl, "JPEG") print('out=', type(out)) detectfaces = faceApi.detectLocalImage(pictureurl) # if len(detectfaces) == 0: # print('相片中找不到人!') # sys.exit(1) faceids = [] for detectface in detectfaces: print('所偵測到的 faceId=', detectface['faceId']) faceids.append(detectface['faceId']) print('Identify.detectfaces=', detectfaces) try: identifiedfaces = faceApi.identify(faceids[:10], personGroupId) print('在所提供的相片中偵測到 identifyfaces 共 ', len(identifiedfaces), '個') except MyException.PersonGroupNotTrainedError as e: print('接到例外!MyException.PersonGroupNotTrainedError as e') print('Identify.detectedFaces=', detectfaces) ClassCV.cv_Identifyfaces(detectfaces, pictureurl) #ClassTK.tk_UnknownPerson('texttest....', pictureurl, pictureurl) return print('在所提供的相片中偵測到 identifyfaces 共 ', len(identifiedfaces), '個') # successes = [] for identifiedface in identifiedfaces: for candidate in identifiedface['candidates']: personId = candidate["personId"] person = personApi.get_a_person(personId, personGroupId) identifiedface['person'] = person identifiedface['confidence'] = candidate["confidence"] identifiedface['personId'] = candidate["personId"] Utils.SigninIdentifyfaces(identifiedfaces, pictureurl)
def __init__(self, message): self.message = message text = 'API KEY 已經失效,請到 config 設定有效的 API KEY。' print(text) if ClassUtils.isLinux(): print(text) else: # import ClassMessageBox # ClassMessageBox.MessageGUI(message, text) print(text) ClassCV.cv_ImageText('「驗證失敗」', text)
def SigninIdentifyfaces(identifyfaces, picture=None): if isLinux(): if len(identifyfaces) == 0: print('照片裡沒有人!') return for identifyface in identifyfaces: if 'person' in identifyface: print("identifyface['confidence']=",identifyface['confidence']) name = protectPersonName(identifyface['person']['name']) textConfidence(name, identifyface['confidence']) else: print('你哪位?', identifyface) elif isWindows() or isDarwin(): import ClassCV ClassCV.cv_Identifyfaces(identifyfaces, picture)
def takePicture_opencv(personGroupId, delay, typee): if (ClassUtils.isWindows() or ClassUtils.isDarwin()): picturepath = ClassCV.show_opencv(typee, mirror=True) return picturepath else: print('若系統為樹莓派,則需設定 camera 為 CSIcamera 無法以 webcam 作為影像來源。') return None