def frame2emocrop(frame, threshold=0.9, qps=2, input_size=(1024, 1024), output_size=(256, 256), save_dir='.', fmt='.png'): sleep(1 / qps) img_base64 = frame2base64(frame) img = cv2.resize(img, input_size) res = client.detect(img_base64, img_type, options) if res['error_code'] != 0: return None for face in res['result']['face_list']: if face['emotion']['probability'] < threshold: continue emotion = face['emotion']['type'] face_token = face['face_token'] loc = face['location'] top = int(loc['top']) bottom = int(loc['top'] + loc['height']) left = int(loc['left']) right = int(loc['left'] + loc['width']) rotation = loc['rotation'] crop = frame[top:bottom, left:right] crop = cv2.resize(crop, output_size) ensure_dir(f'{save_dir}/{emotion}') cv2.imwrite(f'{save_dir}/{emotion}/{face_token}{fmt}', crop) return res
def imcompose(imgs, save_path='compose.png', width=256, height=256): assert len(imgs) >= 1, "The imgs can't be none!" row, col = len(imgs), len(imgs[0]) target = Image.new('RGB', (col * width, row * height)) for i, img_row in enumerate(imgs): for j, img in enumerate(img_row): tmp = Image.open(img).resize((width, height), Image.ANTIALIAS) target.paste(tmp, (j * width, i * height)) ensure_dir(save_path) return target.save(save_path)
def fontset(ttf, chars=None, img_size=[256,256], font_size=240, background="black", bg_value=(255,255,255), start_pos=(8,8), mode='scc', save_dir=''): assert chars != None, 'The chars can not be None.' font = ImageFont.truetype(ttf, font_size) font_dir = ttf.rstrip('.ttf') if save_dir == '' else save_dir ensure_dir(font_dir) for c in chars: try: im = Image.new("RGB", img_size, background) im_draw = ImageDraw.Draw(im) im_draw.text(start_pos, c, bg_value, font) im.save(f"{font_dir}/{c}.png") except: print(f'Process {c} error.') return False return True
def gif2img(infile, save_dir=None): if not save_dir: save_dir = infile.rsplit('.', 1)[0] try: im = Image.open(infile) except IOError: print("Cant load", infile) ensure_dir(save_dir) cnt = 0 palette = im.getpalette() try: while True: if not im.getpalette(): im.putpalette(palette) new_im = Image.new("RGBA", im.size) new_im.paste(im) new_im.save(f'{save_dir}/{cnt}.png') cnt += 1 im.seek(im.tell() + 1) except EOFError: print(f"gif to {cnt} imgs done") return cnt
def im2emocrop(img_path, threshold=0.9, qps=2, input_size=(256, 256), output_size=(256, 256), save_dir='.', align=False, fmt='.png'): sleep(1 / qps) try: img = cv2.imread(img_path) if img.shape != input_size: img = cv2.resize(img, input_size) except Exception as e: print(f'{img_path}:', e) return None # 方式1 # img_base64 = im2base64(img_path) # 方式2 img_base64 = frame2base64(img) res = client.detect(img_base64, img_type, options) if res['error_code'] != 0: return res for face in res['result']['face_list']: if face['emotion']['probability'] < threshold: continue emotion = face['emotion']['type'] face_token = face['face_token'] loc = face['location'] if align: top = int(loc['top']) bottom = int(loc['top'] + loc['height']) left = int(loc['left']) right = int(loc['left'] + loc['width']) rotation = loc['rotation'] crop = img[top:bottom, left:right] else: crop = img if crop.shape != output_size: crop = cv2.resize(crop, output_size) ensure_dir(f'{save_dir}/{emotion}') cv2.imwrite(f'{save_dir}/{emotion}/{face_token}{fmt}', crop) return res
def imresize(img_path, save_dir='.', size=(256, 256)): name = img_path.rsplit('/', 1)[-1] im = Image.open(img_path).resize(size, Image.ANTIALIAS) ensure_dir(save_dir) im.save(f'{save_dir}/{name}')
def align_faces(raw_dir, align_dir, min_input_size=(256,256), output_size=1024, transform_size=1024, x_scale=1, y_scale=1, em_scale=0.1, alpha=False): ensure_dir(align_dir) for img_name in os.listdir(raw_dir): print(f'Aligning {img_name} ...') raw_img_path = os.path.join(raw_dir, img_name) try: img = cv2.imread(raw_img_path) if img.shape[0] < min_input_size[0] or img.shape[1] < min_input_size[1]: continue except: print(f'Read {img_name} error.') continue fn = face_img_name = '%s_%02d.png' % (os.path.splitext(img_name)[0], 1) if os.path.isfile(fn): continue try: print('Getting landmarks...') for i, face_landmarks in enumerate(landmarks_detector.get_landmarks(raw_img_path), start=1): try: print('Starting face alignment...') face_img_name = '%s_%02d.png' % (os.path.splitext(img_name)[0], i) aligned_face_path = os.path.join(align_dir, face_img_name) face_align(raw_img_path, aligned_face_path, face_landmarks, output_size=output_size, transform_size=transform_size, x_scale=x_scale, y_scale=y_scale, em_scale=em_scale, alpha=alpha) print('Wrote result %s' % aligned_face_path) except: print("Exception in face alignment!") except: print("Exception in landmark detection!") def video2img(infile, save_dir=None): if not save_dir: save_dir = infile.rsplit('.', 1)[0] try: vc = cv2.VideoCapture(infile) rval = vc.isOpened() except: print("Cant load", infile) return if not os.path.exists(save_dir): os.mkdir(save_dir) cnt = 0 rval, frame = vc.read() while rval: cv2.imwrite(f'{save_dir}/{cnt}.jpg', frame) cnt += 1 rval, frame = vc.read() print(f"video to {cnt} imgs done") def gif2img(infile, save_dir=None): if not save_dir: save_dir = infile.rsplit('.', 1)[0] try: im = Image.open(infile) except IOError: print("Cant load", infile) ensure_dir(save_dir) cnt = 0 palette = im.getpalette() try: while True: if not im.getpalette(): im.putpalette(palette) new_im = Image.new("RGBA", im.size) new_im.paste(im) new_im.save(f'{save_dir}/{cnt}.png') cnt += 1 im.seek(im.tell() + 1) except EOFError: print(f"gif to {cnt} imgs done") return cnt def fontset(ttf, chars=None, img_size=[256,256], font_size=240, background="black", bg_value=(255,255,255), start_pos=(8,8), mode='scc', save_dir=''): assert chars != None, 'The chars can not be None.' font = ImageFont.truetype(ttf, font_size) font_dir = ttf.rstrip('.ttf') if save_dir == '' else save_dir ensure_dir(font_dir) for c in chars: try: im = Image.new("RGB", img_size, background) im_draw = ImageDraw.Draw(im) im_draw.text(start_pos, c, bg_value, font) im.save(f"{font_dir}/{c}.png") except: print(f'Process {c} error.') return False return True