Exemple #1
0
def cv_Identifyfaces(identifyfaces, picture=None):
    ''' 運用 cv2 技術顯示的 Identifyfaces '''
    import cv2
    import numpy as np
    # print('identifyfaces=',identifyfaces)
    if len(identifyfaces) == 0:
        cv_ImageText('沒有偵測到任何人!', '請按「ENTER」繼續')
        return
    for identifyface in identifyfaces:
        faceimagepath = ClassUtils.getFaceImagepath(identifyface['faceId'])
        if 'person' not in identifyface:
            print('identifyface=', identifyface)
            cv_ImageText('你哪位?請先訓練。', '按 ENTER 繼續', faceimagepath, picture,
                         identifyfaces)
        else:
            text = ClassUtils.textConfidence(identifyface['person']['name'],
                                             identifyface['confidence'])
            try:
                print(text, identifyface['confidence'])
            except UnicodeEncodeError as e:
                print("UnicodeEncodeERROR!!", identifyface['confidence'])
            #print('cv_Identifyfaces.identifyface=', identifyface)
            # text = ClassUtils.textConfidence(identifyface['person']['name'],
            #                                  identifyface['confidence'])
            cv_ImageText(text, '按 ENTER 繼續', faceimagepath, picture,
                         identifyfaces)
Exemple #2
0
def cv_Identifyfaces(identifyfaces, picture=None):
    ''' 運用 cv2 技術顯示的 Identifyfaces '''
    import cv2
    import numpy as np
    # print('identifyfaces=',identifyfaces)
    if len(identifyfaces) == 0:
        cv_ImageText('No Faces Detected', 'Press [ENTER] to continue ')
        return
    for identifyface in identifyfaces:
        faceimagepath = ClassUtils.getFaceImagepath(identifyface['faceId'])
        if 'person' not in identifyface:
            print('identifyface=', identifyface)
            cv_ImageText('Who is it?? Please train first!',
                         'Press [ENTER] to continue ', faceimagepath, picture,
                         identifyfaces)
        else:
            text = ClassUtils.textConfidence(identifyface['person']['name'],
                                             identifyface['confidence'])
            try:
                print(text, identifyface['confidence'])
            except UnicodeEncodeError as e:
                print("UnicodeEncodeERROR!!", identifyface['confidence'])
            #print('cv_Identifyfaces.identifyface=', identifyface)
            # text = ClassUtils.textConfidence(identifyface['person']['name'],
            #                                  identifyface['confidence'])
            cv_ImageText(text, '[ENTER] to continue ', faceimagepath, picture,
                         identifyfaces)
Exemple #3
0
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
Exemple #4
0
def play_gTTS(name, text):
    start = int(round(time.time() * 1000))
    print('開始計時 play_gTTS 0 ms: ', name, text)
    #text = '_'.join(lazy_pinyin(text))
    name = ClassUtils.protectPersonName(name)

    mp3path = os.path.join(mp3base, name + text + ".mp3")
    #print('gTTS:', str(name + text).encode("utf8"), 'mp3path:', mp3path, os.path.isfile(mp3path))
    print('SPEED: play_gTTS mp3path', int(round(time.time() * 1000) - start),
          'ms')

    if os.path.isfile(mp3path) == False:
        tts = gTTS(text=ClassUtils.protectPersonNameForTTS(name) + text,
                   lang='zh-tw')
        tts.save(mp3path)
        print('SPEED: play_gTTS savemp3',
              int(round(time.time() * 1000) - start), 'ms')

    sysstr = platform.system()
    #print('system='+sysstr)
    # print('SPEED: pygame play 前', int(round(time.time() * 1000) - start), 'ms')
    # if (sysstr == "Windows"):
    #     print("Call Windows tasks")
    #     pygamee.mixer.init()
    #     pygamee.mixer.music.load(mp3path)
    #     pygamee.mixer.music.play()
    #     while pygamee.mixer.music.get_busy():
    #         pygamee.time.Clock().tick(10)
    # elif (sysstr == "Darwin"):
    #     print("Call macOS tasks")
    #     pygamee.mixer.init()
    #     pygamee.mixer.music.load(mp3path)
    #     pygamee.mixer.music.play()
    #     while pygamee.mixer.music.get_busy():
    #         pygamee.time.Clock().tick(10)
    # elif (sysstr == "Linux"):
    #     #os.system('omxplayer ' + mp3path +" > /dev/null 2>&1")
    #     pygamee.mixer.init()
    #     pygamee.mixer.music.load(mp3path)
    #     pygamee.mixer.music.play()
    #     while pygamee.mixer.music.get_busy() == True:
    #         continue
    # else:
    print("Call Other OS tasks")
    pygamee.mixer.init()
    pygamee.mixer.music.load(mp3path)
    pygamee.mixer.music.play()
    while pygamee.mixer.music.get_busy():
        pygamee.time.Clock().tick(5)
    print('SPEED: pygame play 後', int(round(time.time() * 1000) - start), 'ms')
Exemple #5
0
    def get_a_person(self, personId, personGroupId):
        headers = {
            # Request headers
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({})

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request(
                "GET", "/face/v1.0/persongroups/" + personGroupId +
                "/persons/" + personId + "?%s" % params, "{body}", headers)
            response = conn.getresponse()
            data = response.read()
            personjson = json.loads(str(data, 'UTF-8'))
            conn.close()

            try:
                if ClassUtils.isFaceAPIError(personjson):
                    return None
            except MyException.RateLimitExceededError as e:
                time.sleep(10)
                return self.get_a_person(personId, personGroupId)
            except MyException.UnspecifiedError as e:
                return
            except MyException.PersonGroupNotTrainedError as e:
                print('ERROR: get_a_person.PersonGroupNotTrainedError')
                return
            return personjson

        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
Exemple #6
0
    def personGroup_status(self, personGroupId):
        print("personGroup_status: 查看一個 personGroup 的狀態,也就是看看訓練是否成功!")
        headers = {
            # Request headers
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({'personGroupId': personGroupId})

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request(
                "GET", "/face/v1.0/persongroups/" + personGroupId +
                "/training?%s" % params, "{body}", headers)
            response = conn.getresponse()
            data = response.read()
            status = json.loads(str(data, 'UTF-8'))
            conn.close()
        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
        try:
            if ClassUtils.isFaceAPIError(status):
                return None
            return status
        except MyException.UnspecifiedError as e:
            return
Exemple #7
0
    def getPersonGroup(self, personGroupId):
        print('搜尋 personGroupid =', personGroupId)
        headers = {
            # Request headers
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({})

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request(
                "GET",
                "/face/v1.0/persongroups/" + personGroupId + "?%s" % params,
                "{body}", headers)
            response = conn.getresponse()
            data = response.read()
            personGroup = json.loads(str(data, 'UTF-8'))
            conn.close()
        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))

        try:
            if ClassUtils.isFaceAPIError(personGroup):
                pass
            return personGroup
        except MyException.UnspecifiedError as e:
            return
Exemple #8
0
    def __detectFaces_Save(self, detectFaces, imagepath):
        for detectface in detectFaces:
            print("faceRectangle = ", detectface['faceRectangle'])
            print("faceId = ", detectface['faceId'])
            left = detectface['faceRectangle']['left']
            top = detectface['faceRectangle']['top']
            height = detectface['faceRectangle']['height']
            width = detectface['faceRectangle']['width']

            img = Image.open(imagepath)
            draw = ImageDraw.Draw(img)
            if config['landmark'] > 0:
                print("save facelandmarks=", detectface['faceLandmarks'])
                for faceLandmark in detectface['faceLandmarks']:
                    print('faceLandmark=', faceLandmark)
                    print('faceLandmark=',
                          detectface['faceLandmarks'][faceLandmark])
                    x = int(detectface['faceLandmarks'][faceLandmark]['x'])
                    y = int(detectface['faceLandmarks'][faceLandmark]['y'])
                    draw.ellipse(
                        (x, y, x + config['landmark'], y + config['landmark']),
                        fill=(255, 0, 0))
            #faceRectangle =  {'top': 141, 'height': 261, 'width': 261, 'left': 664}
            faceonly = img.crop((left, top, left + width, top + height))

            saveFaceImagepath = ClassUtils.getFaceImagepath(
                detectface['faceId'])
            faceonly.save(saveFaceImagepath, 'PNG')
Exemple #9
0
    def ListPersonGroups(self):
        #print('列出所有的 person Groups')
        headers = {
            # Request headers
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({
            # Request parameters
            #'start': '{string}',
            'top': '1000',
        })

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request("GET", "/face/v1.0/persongroups?%s" % params,
                         "{body}", headers)
            response = conn.getresponse()
            data = response.read()
            personGroups = json.loads(str(data, 'UTF-8'))
            conn.close()
        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
        try:
            if ClassUtils.isFaceAPIError(personGroups):
                return []
        except MyException.RateLimitExceededError as e:
            time.sleep(10)
            return self.ListPersonGroups()
        except MyException.UnspecifiedError as e:
            return

        return personGroups
Exemple #10
0
def SuccessesGUI(successes):
    ''' 多位成功者放在同一個視窗 '''
    print('STEP 0')
    root = tk.Tk()
    print('STEP 0.1')
    root.geometry('400x400')
    print('STEP 1')
    scrollbar = tk.Scrollbar(root)
    scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
    print('STEP 2')

    mylist = tk.Listbox(root,
                        font="Helvetica 18 bold",
                        yscrollcommand=scrollbar.set)
    y = 10
    for success in successes:
        #imagepath = ClassUtils.getFaceImagepath(success['faceId'])
        #imagefile = __saveImg(imagepath)
        #canvas = tk.Canvas(root, height=imagefile.height()*len(successes), width=imagefile.width()*5)
        #canvas.create_image(10, y, anchor="nw", image=imagefile)
        #y += 10
        mylist.insert(
            tk.END,
            ClassUtils.protectPersonName(success['person']['name']) + '簽到成功!!')
    print('STEP 3')

    #canvas.pack()
    mylist.pack(side=tk.LEFT, fill=tk.BOTH)
    scrollbar.config(command=mylist.yview)

    print('STEP 4')
    root.call('wm', 'attributes', '.', '-topmost', '1')
    root.mainloop()
Exemple #11
0
def takePicture_CSI(personGroupId, delay, size='small'):
    # delay in ms 3000ms = 3s
    # jpgimagepath = os.path.join(basepath, 'takepictures', personGroupId + "_" + time.strftime(
    #     "%Y%m%d_%H%M%S", time.localtime()) + ".jpg")
    picturepath = ClassUtils.getTakePicturePath(personGroupId)
    if not os.path.exists(os.path.dirname(picturepath)):
        os.makedirs(os.path.dirname(picturepath))
    try:
        # small for 辨識,加快速度。
        if size == 'small':
            subprocess.call([
                'raspistill', '-hf', '-w', '800', '-h', '450', '-t',
                str(delay), '-o', picturepath
            ])
        else:  # for 訓練。訓練用圖片可以比較大
            subprocess.call([
                'raspistill', '-hf', '-w', '1600', '-h', '900', '-t',
                str(delay), '-o', picturepath
            ])

    except OSError:
        # ClassMessageBox.FaceAPIErrorGUI('def takePicture_CSI', 'CSI 攝影機無法啟動!',
        #                                 'OSError: raspistill 無法執行或不存在!!')
        print('def takePicture_CSI', 'CSI 攝影機無法啟動!',
              'OSError: raspistill 無法執行或不存在!!')
        return None

    #os.system("raspistill -t " + str(delay) + " -o " + imagepath)
    return picturepath
Exemple #12
0
def takePicture(personGroupId, delay, type='Identify', size='small'):
    sysstr = platform.system()
    print('os=', sysstr, platform.release())

    if ClassUtils.isLinux():
        return takePicture_CSI(personGroupId, delay, size)
    else:
        return takePicture_opencv(personGroupId, delay, type)
Exemple #13
0
    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)
Exemple #14
0
 def totalDistance(self):
     # AIzaSyA7_9tSb7YwCpul5IF_edGnqGUO6Q1Zed8 for Geocoding
     backToBacks = self.findBackToBacks()
     if len(backToBacks) > 0:
         self.distance = 0
         for classPair in backToBacks:
             distance = ClassUtils.findDistanceBetweenTwoClasses(classPair)
             self.distance += distance
     return self.distance
Exemple #15
0
    def identify(self, faceidkeys, personGroupId):
        print("def Face.identify 開始辨識。faceidkeys=", faceidkeys)
        if len(faceidkeys) == 0:
            return []
        start = int(round(time.time() * 1000))
        print('開始辨識 identify 0 ms')

        headers = {
            # Request headers
            'Content-Type': 'application/json',
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({})

        requestbody = '''{
            "personGroupId": "''' + personGroupId + '''",
            "faceIds":''' + str(faceidkeys) + ''',
            "maxNumOfCandidatesReturned":1,
            "confidenceThreshold": ''' + str(config['confidence']) + '''
        }'''
        #print('requestbody=', requestbody)
        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request("POST", "/face/v1.0/identify?%s" % params,
                         requestbody, headers)
            response = conn.getresponse()
            data = response.read()
            identifiedfaces = json.loads(str(data, 'UTF-8'))
            print('Face.Identify.identifiedfaces=', identifiedfaces)
            conn.close()
            # ClassUtils.tryFaceAPIError(identifyfaces)
        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
            sys.exit()

        try:
            if ClassUtils.isFaceAPIError(identifiedfaces):
                return []
        except MyException.RateLimitExceededError as e:
            time.sleep(10)
            return self.identify(faceidkeys, personGroupId)
        except MyException.PersonGroupNotFoundError as e:
            personGroupAPI = PersonGroup(self.api_key, self.host)
            personGroupAPI.createPersonGroup(personGroupId,
                                             config['personGroupName'],
                                             'group userdata')
            return self.identify(faceidkeys, personGroupId)
        except MyException.UnspecifiedError as e:
            return []
        except MyException.PersonGroupNotTrainedError as e:
            print('丟出 MyException.PersonGroupNotTrainedError 例外')
            raise
        print('超過 raise')
        # if ClassUtils.isFaceAPIError(identifyfaces):
        #     return []
        return identifiedfaces
Exemple #16
0
    def detectLocalImage(self, imagepath):
        start = int(round(time.time() * 1000))
        print('開始計時 detectLocalImage 0 ms')

        headers = {
            # Request headers
            'Content-Type': 'application/octet-stream',  # 用本地圖檔辨識
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({
            # Request parameters
            'returnFaceId':
            'true',
            'returnFaceLandmarks':
            'true',
            'returnFaceAttributes':
            'age,gender,emotion'
        })
        #'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure'
        print('imagepath=', imagepath)
        requestbody = open(imagepath, "rb").read()
        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request("POST", "/face/v1.0/detect?%s" % params, requestbody,
                         headers)
            response = conn.getresponse()
            data = response.read()
            print('detectLocalImage.data=', data)
            detectfaces = json.loads(str(data, 'UTF-8'))
            print("detectLocalImage.faces=", detectfaces)
            #print(parsed[0]['faceId'])
            #faceids.append(parsed[0]['faceId'])
            conn.close()

            try:
                if ClassUtils.isFaceAPIError(detectfaces):
                    return []
            except MyException.RateLimitExceededError as e:
                time.sleep(10)
                return self.detectLocalImage(imagepath)
            except MyException.PersonGroupNotTrainedError as e:
                print(
                    'ERROR: detectLocalImage MyException.PersonGroupNotTrainedError'
                )
                return
            except MyException.UnspecifiedError as e:
                return

            print("detectLocalImage:",
                  imagepath + "偵測到 {0} 個人".format(len(detectfaces)))

            self.__detectFaces_Save(detectfaces, imagepath)
            return detectfaces

        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
Exemple #17
0
 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)
Exemple #18
0
    def add_a_person_face(self, imagepath, personId, personGroupId):
        print("'add_a_person_face': 用一個圖片放入一個 person 當中 personId=" + personId,
              'imagepath=', imagepath)
        #display(Image(url=imagepath))

        headers = {
            # Request headers
            #        'Content-Type': 'application/json',
            'Content-Type': 'application/octet-stream',  #上傳圖檔
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({
            # Request parameters
            'personGroupId': personGroupId,
            #'personId': '03cb1134-ad35-4b80-8bf2-3200f44eef31',
            'personId': personId,
            #'userData': '{string}',
            #'targetFace': '{string}',
        })
        #"https://lh3.googleusercontent.com/AuJtzSdWCTZ6pWW9pMxec86gVZEjP00O7qvl8RNbzYfmJvsiUfDL-BXfel5Sw2jgPNUy7rcIVQ-HFDxDEFuIZxp56NpKwOjYncgMjL_dt0-FnoBIYyUpplx4LlE5ShN2hJ3-URLwOA4=w597-h796-no"

        #    requestbody = '{"url": "'+imageurl+'"}'
        requestbody = open(imagepath, "rb").read()

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request(
                "POST", "/face/v1.0/persongroups/" + personGroupId +
                "/persons/" + personId + "/persistedFaces?%s" % params,
                requestbody, headers)
            response = conn.getresponse()
            data = response.read()
            jsondata = json.loads(str(data, 'UTF-8'))
            conn.close()
            # if 'error' in jsondata:
            #     if 'RateLimitExceeded' == jsondata['error']['code']:
            #         time.sleep(10)
            #     else:
            #         print("EXCEPTION: ", jsondata['error']['code'] + ":",
            #               jsondata['error']['message'])

        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))

        try:
            if ClassUtils.isFaceAPIError(jsondata):
                return []
        except MyException.RateLimitExceededError as e:
            time.sleep(10)
            return self.add_a_person_face(imagepath, personId, personGroupId)
        except MyException.UnspecifiedError as e:
            return
Exemple #19
0
    def traindatas(self, traindatasPath):
        ''' 請輸入 traindatasPath 的絕對路徑。
        請提供資料路徑如: /xxx/xxx/traindatas/
        該路徑內的資料夾結構必須為 [userData/姓名/xxxx.jpg]
        '''
        #traindataPath = basepath + '/traindatas/'
        #traindataPath = os.path.join(basepath, 'traindatas')
        Utils.makedirsPath(traindatasPath)
        train_userDataPaths = os.listdir(traindatasPath)
        print('目前 traindatas/ 內的圖檔如下:')

        for userDataPath in train_userDataPaths:
            userDataPath = os.path.join(traindatasPath, userDataPath)
            if os.path.basename(userDataPath).startswith('.') or os.path.isdir(
                    userDataPath) == False:
                print(userDataPath)
                print('continue')
                continue

            for personnamePath in os.listdir(userDataPath):
                #print("file="+ os.path.join(traindataPath, trainfile))
                personname = os.path.basename(personnamePath)
                personpath = os.path.join(traindatasPath, userDataPath,
                                          personname)
                if os.path.isdir(personpath):
                    print("person name=", personname)
                    personImagePaths = []
                    for personImagePath in os.listdir(personpath):
                        personImagePaths.append(
                            os.path.join(personpath, personImagePath))
                    print(personGroupId, personname, personImagePaths)

                    personAPI = FaceAPI.Person(api_key, host)
                    personAPI.add_personimages(personGroupId, personname,
                                               os.path.basename(userDataPath),
                                               personImagePaths)
                    #time.sleep(6)

        personGroupapi = FaceAPI.PersonGroup(api_key, host)
        personGroupapi.train_personGroup(personGroupId)
Exemple #20
0
def cv_ImageText(title, hint, facepath=None, picture=None, identifyfaces=None):
    ''' 標準 cv 視窗'''
    import cv2
    import numpy as np
    if facepath == None:
        img = np.zeros((400, 400, 3), np.uint8)
        img.fill(90)
    else:
        img = cv2.imread(facepath)
        print('__cv_ImageText.imagepath=', facepath)
        H, W = img.shape[:2]
        img = cv2.resize(img, (400, int(H / W * 400)))

    windowname = facepath
    H, W = img.shape[:2]

    #img = cv2.resize(img, (400,int(H/W*400)))

    ttf = ClassUtils.getSystemFont()

    cv2_im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # cv2和PIL中颜色的hex码的储存顺序不同
    pil_im = Image.fromarray(cv2_im)
    draw = ImageDraw.Draw(pil_im)  # 括号中为需要打印的canvas,这里就是在图片上直接打印
    titlefont = ImageFont.truetype(ttf, 24, encoding="utf-8")
    hintfont = ImageFont.truetype(ttf, 18, encoding="utf-8")

    w, h = draw.textsize(title, font=titlefont)
    draw.rectangle(((W / 2 - w / 2 - 5, 0), (W / 2 + w / 2 + 5, h + 20)),
                   fill="black")
    titlelocation = (W / 2 - w / 2, 5)

    if identifyfaces != None and len(identifyfaces) == 1:
        hint = hint + "or [a] for new ID"
    w, h = draw.textsize(hint, font=hintfont)
    draw.rectangle(((W / 2 - w / 2 - 5, H - h), (W / 2 + w / 2 + 5, H)),
                   fill="red")
    hintlocation = (W / 2 - w / 2, H - h)
    draw.text(titlelocation, title, (0, 255, 255), font=titlefont)
    draw.text(hintlocation, hint, (0, 255, 0), font=hintfont)

    cv2_text_im = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)
    cv2.imshow(windowname, cv2_text_im)
    key = cv2.waitKey(10000)
    if key == ord(' ') or key == 3 or key == 13:  # space or enter
        cv2.destroyWindow(windowname)
    elif key == ord('a') and len(identifyfaces) == 1:  # 鍵盤 a 代表要新增 oneshot
        cv2.destroyWindow(windowname)
        ClassTK.tk_UnknownPerson('您哪位?', facepath, picture)
Exemple #21
0
    def detectURLImages_NoDownload(self, imageurl):
        headers = {
            # Request headers
            'Content-Type': 'application/json',  # 
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({
            # Request parameters
            'returnFaceId':
            'true',
            'returnFaceLandmarks':
            'false',
            'returnFaceAttributes':
            'age,gender,emotion'
        })
        #'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure'
        print('imageurl=', imageurl)
        requestbody = '{"url": "' + imageurl + '"}'

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request("POST", "/face/v1.0/detect?%s" % params, requestbody,
                         headers)
            response = conn.getresponse()
            data = response.read()
            detectfaces = json.loads(str(data, 'UTF-8'))
            print("detecURLImage.faces 偵測到 faces 長度=", len(detectfaces))
            for index, face in enumerate(detectfaces):
                print('face[' + str(index) + ']=', face)
            conn.close()
        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
            #return []

        try:
            if ClassUtils.isFaceAPIError(detectfaces):
                return []
        except MyException.RateLimitExceededError as e:
            time.sleep(10)
            return self.detectURLImages(imageurl)
        except MyException.UnspecifiedError as e:
            return

        self.__detectFaces_Save(detectfaces, imageurl)
        return detectfaces
Exemple #22
0
    def list_persons_in_group(self, personGroupId):
        headers = {
            # Request headers
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({
            # Request parameters
            #'start': '{string}',
            #'top': '1000',
        })

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request(
                "GET", "/face/v1.0/persongroups/" + personGroupId +
                "/persons?%s" % params, "{body}", headers)

            response = conn.getresponse()
            data = response.read()
            persons = json.loads(str(data, 'UTF-8'))
            conn.close()

            try:
                if ClassUtils.isFaceAPIError(persons):
                    pass
            except MyException.RateLimitExceededError as e:
                time.sleep(10)
                return self.list_persons_in_group(personGroupId)
            except MyException.PersonGroupNotFoundError as e:
                self.createPersonGroup(config['personGroupId'],
                                       config['personGroupName'],
                                       'group userdata')
                return self.list_persons_in_group(config['personGroupId'])

            if 'error' in persons:
                message = '取得 persons 出錯!\n'
                message += '錯誤編號 = ' + persons['error']['code'] + '\n'
                message += '錯誤訊息 = ' + persons['error']['message']
                raise MyException.responseError(message)
            return persons

        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
Exemple #23
0
    def list_persons_in_group(self, personGroupId):
        print("'list_persons_in_group'")
        headers = {
            # Request headers
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({
            # Request parameters
            #'start': '{string}',
            #'top': '1000',
        })

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request(
                "GET", "/face/v1.0/persongroups/" + personGroupId +
                "/persons?%s" % params, "{body}", headers)
            response = conn.getresponse()
            data = response.read()
            persons = json.loads(str(data, 'UTF-8'))
            #print('def list_persons_in_group:', persons)
            conn.close()
        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))
            return []

        try:
            if ClassUtils.isFaceAPIError(persons):
                return []
        except MyException.RateLimitExceededError as e:
            time.sleep(10)
            return self.list_persons_in_group(personGroupId)
        except MyException.PersonGroupNotFoundError as e:
            personGroupAPI = PersonGroup(self.api_key, self.host)
            personGroupAPI.createPersonGroup(config['personGroupId'],
                                             config['personGroupName'],
                                             'group userdata')
            return self.list_persons_in_group(personGroupId)
        except MyException.UnspecifiedError as e:
            return

        return persons
Exemple #24
0
    def create_a_person(self, personGroupId, name, userData):
        print("'create_a_person': 在 personGroupid=" + personGroupId +
              " 裡 建立一個 person name=" + name)
        headers = {
            # Request headers
            'Content-Type': 'application/json',
            'Ocp-Apim-Subscription-Key': self.api_key,
        }

        params = urllib.parse.urlencode({'personGroupId': personGroupId})
        requestbody = '{"name":"' + name + '","userData":"' + userData + '"}'

        try:
            conn = http.client.HTTPSConnection(self.host)
            conn.request(
                "POST", "/face/v1.0/persongroups/" + personGroupId +
                "/persons?%s" % params, requestbody.encode('UTF-8'), headers)
            response = conn.getresponse()
            data = response.read()
            create_a_person_json = json.loads(str(data, 'UTF-8'))
            conn.close()
        except Exception as e:
            print("[Errno {0}]連線失敗!請檢查網路設定。 {1}".format(e.errno, e.strerror))

        try:
            if ClassUtils.isFaceAPIError(create_a_person_json):
                return []
        except MyException.RateLimitExceededError as e:
            time.sleep(10)
            return self.create_a_person(personGroupId, name, userData)
        except MyException.PersonGroupNotFoundError as e:
            personGroupApi = PersonGroup(self.api_key, self.host)
            personGroupApi.createPersonGroup(config['personGroupId'],
                                             config['personGroupName'],
                                             'group userdata')
            return self.create_a_person(personGroupId, name, userData)
        except MyException.UnspecifiedError as e:
            return

        return create_a_person_json['personId']
Exemple #25
0
def show_opencv(typee, mirror=False):
    ''' 顯示主畫面 '''
    import cv2
    import numpy as np
    config = ClassUtils.loadConfig()

    cam = cv2.VideoCapture(config['videoid'])
    cam.set(3, 1280)  # 修改解析度 寬
    cam.set(4, 1280 // 16 * 9)  # 修改解析度 高
    print('WIDTH', cam.get(3), 'HEIGHT', cam.get(4))  # 顯示預設的解析度
    while True:
        ret_val, img = cam.read()

        # Add for face detection, ottowei , 2019011701
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # Load the Haar cascade file
        face_cascade = cv2.CascadeClassifier(
            'haar_cascade_files/haarcascade_frontalface_default.xml')
        # Run the face detector on the grayscale image
        face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)

        # Draw a rectangle around the face
        for (x, y, w, h) in face_rects:
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3)

        if mirror:
            img = cv2.flip(img, 1)

        H, W = img.shape[:2]
        #imS = cv2.resize(img, (W, H))

        cv2_im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # cv2和PIL中颜色的hex码的储存顺序不同
        pil_im = Image.fromarray(cv2_im)
        draw = ImageDraw.Draw(pil_im)  # 括号中为需要打印的canvas,这里就是在图片上直接打印

        ttf = ClassUtils.getSystemFont()

        font = ImageFont.truetype(ttf, 40, encoding="utf-8")
        hintfont = ImageFont.truetype(ttf, 24, encoding="utf-8")

        title = config['title'] + ""
        w, h = draw.textsize(title, font=font)
        draw.rectangle(((W / 2 - w / 2 - 5, 0), (W / 2 + w / 2 + 5, h + 20)),
                       fill="black")
        titlelocation = (W / 2 - w / 2, 5)
        #textlocation = (0,0)
        draw.text(titlelocation, title, (0, 255, 255),
                  font=font)  # 第一个参数为打印的坐标,第二个为打印的文本,第三个为字体颜色,第四个为字体
        # FreeAPIKEY: b9160fbd882f47bd821205a4bce64354
        if config['api_key'] == 'b9160fbd882f47bd821205a4bce64354':
            warningfont = ImageFont.truetype(ttf, 24, encoding="utf-8")
            warning = "請注意,您目前是用的是共用的測試 API_KEY 請儘速自行申請一個自用的 KEY"
            w, h = draw.textsize(warning, font=warningfont)
            draw.rectangle(
                ((W / 2 - w / 2 - 5, H - h * 2), (W / 2 + w / 2 + 5, H - h)),
                fill="yellow")
            warninglocation = (W / 2 - w / 2, H - h * 2)
            draw.text(
                warninglocation, warning, (0, 0, 255),
                font=warningfont)  # 第一个参数为打印的坐标,第二个为打印的文本,第三个为字体颜色,第四个为字体

        if typee == 'Identify':
            hint = "Please Press [ENTER] to Identify"
        elif typee == 'Train':
            hint = "Please Press [ENTER] to take triple photos"
        else:
            hint = "Please Press [ENTER] to continue"
        w, h = draw.textsize(hint, font=hintfont)
        draw.rectangle(((W / 2 - w / 2 - 5, H - h), (W / 2 + w / 2 + 5, H)),
                       fill="red")
        hintlocation = (W / 2 - w / 2, H - h)
        #textlocation = (0,0)
        draw.text(hintlocation, hint, (0, 255, 255),
                  font=hintfont)  # 第一个参数为打印的坐标,第二个为打印的文本,第三个为字体颜色,第四个为字体

        cv2_text_im = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)

        if ClassUtils.isWindows():
            cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
            cv2.setWindowProperty("window", cv2.WND_PROP_FULLSCREEN,
                                  cv2.WINDOW_FULLSCREEN)

        cv2.imshow("window", cv2_text_im)
        #cv2.imshow("window", img)

        key = cv2.waitKey(1)
        if key == ord(' ') or key == 3 or key == 13:  # space or enter
            picturepath = ClassUtils.getTakePicturePath(
                config['personGroupId'])
            cv2.imwrite(picturepath, img)
            cv2.destroyAllWindows()
            cv2.VideoCapture(config['videoid']).release()
            return picturepath
        elif key == 27:  # esc to quit
            cv2.destroyAllWindows()
            cv2.VideoCapture(config['videoid']).release()
            raise MyException.esc_opencv("偵測到 esc 結束鏡頭")
        else:
            if key != -1:
                print('key=', key)
Exemple #26
0
 def detectURLImages(self, imageurl):
     ''' 下載後,用 detectLocalImage 上傳辨識 '''
     jpgimagepath = ClassUtils.getTakePicturePath(config['personGroupId'])
     print('jpgimagepath:', jpgimagepath)
     request.urlretrieve(imageurl, jpgimagepath)
     return self.detectLocalImage(jpgimagepath)
Exemple #27
0
#!/usr/bin/env python

import sys, os, json, time, fire
from PIL import Image
import ClassFaceAPI as FaceAPI
import ClassCamera as Camera
import ClassUtils as Utils
from pypinyin import lazy_pinyin
import MyException, ClassTK, ClassCV

basepath = os.path.dirname(os.path.realpath(__file__))
config = Utils.loadConfig()
personGroupId = config['personGroupId']
api_key = config['api_key']
host = config['host']


class FacePI:
    ''' FacePI 文字介面
    搭配參數如下:
    createGroup: 建立一個 PersonGroup
    deleteGroup: 刪除一個 PersonGroup
    deletePerson: 刪除 PersonGroup 裡的一個 Person
    listGroups: 列出所有的 PersonGroups
    listPersons: 列出「人群」裡有哪些 Person
    relay: 設定繼電器僅適用樹莓派,
    status: 觀察 PersonGroup status
    search: 搜尋一個personName,
    traindatas: '訓練 /traindatas 裡的圖檔,同時訓練一群事先準備好的人與照片',

    Config: 列出 Config.json 設定。