def faceset(face_tokens, set_name=faceset_name): """see https://console.faceplusplus.com/documents/6329329""" url = 'https://api-us.faceplusplus.com/facepp/v3/faceset/create' files = { 'api_key': (None, util.get_property("gest_api_key")), 'api_secret': (None, util.get_property("gest_api_secret")), 'outer_id': (None, set_name), 'force_merge': (None, '1'), 'face_tokens': (None, ",".join(face_tokens)) } x = requests.post(url, files=files)
def _getMetadataFromLastfm(self, artist, track): api_key = util.get_property("audio_api_key") url = f'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={api_key}&' url += urllib.parse.urlencode({"artist": artist}) + '&' url += urllib.parse.urlencode({"track": track}) + '&' url += '&format=json' resp = urllib.request.urlopen(url) metadata = dict() data = json.loads(resp.read()) if 'track' != list(data.keys())[0]: return None try: metadata['track_title'] = data['track']['name'] metadata['artist'] = data['track']['artist']['name'] metadata['album'] = data['track']['album']['title'] metadata['album_art_url'] = data['track']['album']['image'][-1][ '#text'] except: return None return metadata
def test_gesture_detection(): url = 'https://api-us.faceplusplus.com/humanbodypp/v1/gesture' pictures = [] num_hands = 0 correct_gestures = 0 gestures_count = defaultdict(list) for g in GESTURES: pictures += glob.glob(GALLERY_PATH + f"*{g}*") print("len pictures 1", len(pictures)) # random.shuffle(pictures) # debug # pictures = pictures[:10] # debug # print("len pictures 2", len(pictures)) for p in pictures: gt_user, gt_gesture0 = os.path.splitext( os.path.basename(p))[0].split("_")[0:2] gt_gesture = GESTURES[gt_gesture0] files = { 'api_key': (None, util.get_property("gest_api_key")), 'api_secret': (None, util.get_property("gest_api_secret")), 'image_file': (p, open(p, 'rb')), 'return_gesture': (None, '1'), } x = requests.post(url, files=files) hands = json.loads(x.text)['hands'] for h in hands: num_hands += 1 gesture = Counter(h["gesture"]).most_common(1)[0][0] gestures_count[gt_gesture] += [gesture] if gt_gesture == gesture: correct_gestures += 1 print(f"{gt_user},{gt_gesture},{gesture}") time.sleep(1.1) gestures_matrix = dict() for g in gestures_count: gestures_matrix[g] = Counter(gestures_count[g]) print("num hands:", num_hands) print( f"Correct gestures (accuracy): {correct_gestures}/{len(pictures)}: {correct_gestures / len(pictures)};" ) pprint.pprint(gestures_matrix)
def detect(img_name): """see https://console.faceplusplus.com/documents/5679127""" url = 'https://api-us.faceplusplus.com/facepp/v3/detect' files = { 'api_key': (None, util.get_property("gest_api_key")), 'api_secret': (None, util.get_property("gest_api_secret")), 'image_file': (img_name, open(img_name, 'rb')), 'return_attributes': (None, 'smiling,emotion'), } x = requests.post(url, files=files) res = json.loads(x.text) if "faces" in res: face_token = res['faces'][0]['face_token'] smile = res['faces'][0]['attributes']['smile'] emotions = res['faces'][0]['attributes']['emotion'] emotion = Counter(emotions).most_common(1)[0][0] return face_token, smile, emotion else: return None, None, None
def search(face_token, set_name=faceset_name, t='1e-4'): """see https://console.faceplusplus.com/documents/5681455""" url = 'https://api-us.faceplusplus.com/facepp/v3/search' files = { 'api_key': (None, util.get_property("gest_api_key")), 'api_secret': (None, util.get_property("gest_api_secret")), 'outer_id': (None, set_name), 'face_token': (None, face_token) } x = requests.post(url, files=files) res = json.loads(x.text) # print(res) try: threshold = res['thresholds'][t] confidence = res['results'][0]['confidence'] match_face_token = res['results'][0]['face_token'] # print(threshold, confidence, match_face_token) if confidence >= threshold: return match_face_token else: return None except: return None
import requests import json from collections import Counter from core import util """ using Face++ API https://www.faceplusplus.com/face-searching/ """ faceset_name = util.get_property("faceset") def detect(img_name): """see https://console.faceplusplus.com/documents/5679127""" url = 'https://api-us.faceplusplus.com/facepp/v3/detect' files = { 'api_key': (None, util.get_property("gest_api_key")), 'api_secret': (None, util.get_property("gest_api_secret")), 'image_file': (img_name, open(img_name, 'rb')), 'return_attributes': (None, 'smiling,emotion'), } x = requests.post(url, files=files) res = json.loads(x.text) if "faces" in res: face_token = res['faces'][0]['face_token'] smile = res['faces'][0]['attributes']['smile'] emotions = res['faces'][0]['attributes']['emotion'] emotion = Counter(emotions).most_common(1)[0][0] return face_token, smile, emotion else:
def web_capture(self): time.sleep(3) print("start web capture") cam = cv.VideoCapture(0) img_counter = 0 ts = time.time() while self.running_web: ret = cam.grab() if not ret: print("failed to grab frame") continue if time.time() - ts >= DELAY: ret, frame = cam.retrieve() if not ret: print("failed to retrieve frame") continue img_name = f"images/test-img/frame_{img_counter}.png" cv.imwrite(img_name, frame) print(ts, f"{img_name} written!") if self.youtube.entry.is_visible(): url = 'https://api-us.faceplusplus.com/humanbodypp/v1/gesture' files = { 'api_key': (None, util.get_property("gest_api_key")), 'api_secret': (None, util.get_property("gest_api_secret")), 'image_file': (img_name, open(img_name, 'rb')), 'return_gesture': (None, '1'), } x = requests.post(url, files=files) hands = json.loads(x.text)['hands'] print("hands:", hands) for h in hands: gesture = Counter(h["gesture"]).most_common(1)[0][0] if gesture == "hand_open": print(gesture, "-> pause") t = self.youtube.entry.get_text() if t == ' ' or t == '': self.youtube.entry.set_text("") GLib.idle_add(self.youtube.play, None) self.show_info("Play/Pause") elif gesture == "index_finger_up": print(gesture, "-> next song") GLib.idle_add(self.youtube.next, None) self.show_info("Next") elif gesture == "victory" or gesture == "double_finger_up": print(gesture, "-> previous song") GLib.idle_add(self.youtube.previous, None) self.show_info("Previous") elif gesture == "thumb_up": print(gesture, "-> volume up") GLib.idle_add(self.youtube.volume_up, None) self.show_info("Volume up") elif gesture == "thumb_down": print(gesture, "-> volume down") GLib.idle_add(self.youtube.volume_down, None) self.show_info("Volume down") elif gesture == "fist": print(gesture, "-> mute") GLib.idle_add(self.youtube.toggle_mute, None) self.show_info("Mute") else: print(gesture, "-> nothing") elif self.can_register: try: face_token, smile, emotion = face.detect(img_name) if face_token is None: continue print("emotion:", emotion) except: GLib.idle_add(self.infoLabel.set_text, "Face detection error.") continue match = face.search(face_token) if match is None: continue print("match:", match) match = hashlib.sha256(match.encode()).hexdigest() cursor = util.execute_query( f"SELECT username, deaf FROM users WHERE faces LIKE '%{match}%';" ) res = cursor.fetchall() user = res[0][0] deaf = res[0][1] print("res:", user, deaf) # Login login.go_to_playlist(self.login, user, deaf, emotion) img_counter += 1 ts = time.time()
def mic_capture(self): print(" & start mic capture") def stop_cb(evt): print('CLOSING on {}'.format(evt)) self.running_mic = False def handle_event(evt): if evt.result.text == "play": print(evt.result.text, "-> play") GLib.idle_add(self.youtube.play, None) self.show_info("Play") elif evt.result.text == "stop" or evt.result.text == "pause": print(evt.result.text, "-> stop/pause") GLib.idle_add(self.youtube.play, None) self.show_info("Pause") elif evt.result.text == "next" or evt.result.text == "skip": print(evt.result.text, "-> next song") GLib.idle_add(self.youtube.next, None) self.show_info("Next") elif evt.result.text == "previous": print(evt.result.text, "-> previous song") GLib.idle_add(self.youtube.previous, None) self.show_info("Previous") elif "up" in evt.result.text: print(evt.result.text, "-> volume up") GLib.idle_add(self.youtube.volume_up, None) self.show_info("Volume up") elif "down" in evt.result.text: print(evt.result.text, "-> volume down") GLib.idle_add(self.youtube.volume_down, None) self.show_info("Volume down") elif "mute" in evt.result.text: print(evt.result.text, "-> mute") GLib.idle_add(self.youtube.toggle_mute, None) self.show_info("Mute/Unmute") else: print(evt.result.text, "-> nothing") speech_config = speechsdk.SpeechConfig( subscription=util.get_property("speech_key_1"), region=util.get_property("service_region")) speech_recognizer = speechsdk.SpeechRecognizer( speech_config=speech_config) speech_recognizer.recognizing.connect(handle_event) speech_recognizer.session_started.connect( lambda evt: print('SESSION STARTED: {}'.format(evt))) speech_recognizer.session_stopped.connect( lambda evt: print('SESSION STOPPED {}'.format(evt))) speech_recognizer.canceled.connect( lambda evt: print('CANCELED {}'.format(evt))) # stop continuous recognition on either session stopped or canceled events speech_recognizer.session_stopped.connect(stop_cb) speech_recognizer.canceled.connect(stop_cb) # Start continuous speech recognition speech_recognizer.start_continuous_recognition() while self.running_mic: # print("mic sleep") time.sleep(.5)
def cam_capture(self): time.sleep(3) print("start web capture") img_counter = 0 while self.running_web: img_name = f"images/test-img/frame_{img_counter}.jpg" cmd = f"gphoto2 --capture-image-and-download --filename {img_name} --force-overwrite" args = shlex.split(cmd) s = subprocess.Popen(args, stderr=subprocess.PIPE) s.wait() err = s.stderr.read() if len(err) != 0: print("failed to take picture") continue else: img_counter += 1 print(f"{img_name} written!", "\n", s.stdout) if self.youtube.entry.is_visible(): url = 'https://api-us.faceplusplus.com/humanbodypp/v1/gesture' files = { 'api_key': (None, util.get_property("gest_api_key")), 'api_secret': (None, util.get_property("gest_api_secret")), 'image_file': (img_name, open(img_name, 'rb')), 'return_gesture': (None, '1'), } x = requests.post(url, files=files) hands = json.loads(x.text)['hands'] print("hands:", hands) for h in hands: gesture = Counter(h["gesture"]).most_common(1)[0][0] if gesture == "hand_open": print(gesture, "-> pause") t = self.youtube.entry.get_text() if t == ' ' or t == '': self.youtube.entry.set_text("") GLib.idle_add(self.youtube.play, None) self.show_info("Play/Pause") elif gesture == "index_finger_up": print(gesture, "-> next song") GLib.idle_add(self.youtube.next, None) self.show_info("Next") elif gesture == "victory" or gesture == "double_finger_up": print(gesture, "-> previous song") GLib.idle_add(self.youtube.previous, None) self.show_info("Previous") elif gesture == "thumb_up": print(gesture, "-> volume up") GLib.idle_add(self.youtube.volume_up, None) self.show_info("Volume up") elif gesture == "thumb_down": print(gesture, "-> volume down") GLib.idle_add(self.youtube.volume_down, None) self.show_info("Volume down") elif gesture == "fist": print(gesture, "-> mute") GLib.idle_add(self.youtube.toggle_mute, None) self.show_info("Mute") else: print(gesture, "-> nothing") elif self.can_register: try: face_token, smile, emotion = face.detect(img_name) if face_token is None: continue print("emotion:", emotion) except: GLib.idle_add(self.infoLabel.set_text, "Face detection error.") continue match = face.search(face_token) if match is None: continue print("match:", match) match = hashlib.sha256(match.encode()).hexdigest() cursor = util.execute_query( f"SELECT username, deaf FROM users WHERE faces LIKE '%{match}%';" ) res = cursor.fetchall() user = res[0][0] deaf = res[0][1] print("res:", user, deaf) # Login login.go_to_playlist(self.login, user, deaf, emotion) time.sleep(DELAY)