def recognize_from_image(net, net_s8): # prepare mask image if not args.generate_mask: mask_path = args.mask_image mask_img = load_image(mask_path) mask_img = cv2.cvtColor(mask_img, cv2.COLOR_BGRA2GRAY) # input image loop for image_path in args.input: # prepare input data logger.info(image_path) # prepare input data img = load_image(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) # generate mask if args.generate_mask: check_and_download_models(MASK_WEIGHT_PATH, MASK_MODEL_PATH, MASK_REMOTE_PATH) mask_net = ailia.Net(MASK_MODEL_PATH, MASK_WEIGHT_PATH, env_id=args.env_id) mask_output = generate_mask(mask_net, img) mask_img = mask_output[:,:,0] cv2.imwrite("generated_mask.png",mask_output) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time_estimation = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) output = predict(net, net_s8, img, mask_img) end = int(round(time.time() * 1000)) estimation_time = (end - start) # Loggin logger.info(f'\tailia processing estimation time {estimation_time} ms') if i != 0: total_time_estimation = total_time_estimation + estimation_time logger.info(f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms') else: output = predict(net, net_s8, img, mask_img) # postprocessing res_img = (output * 255).astype(np.uint8) if args.composite: img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA) img[:,:,3] = res_img res_img = img savepath = get_savepath(args.savepath, image_path, ext='.png') logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) logger.info('Script finished successfully.')
def recognize_from_image(GMM_net, TOM_net, det_net, pose_net, seg_net): img = load_image(args.person) if det_net: img, offset, scale = human_detect(det_net, img) agnostic = cloth_agnostic(pose_net, seg_net, img) agnostic = np.expand_dims(agnostic, axis=0) # input image loop for image_path in args.input: logger.info(image_path) # prepare cloth image img = load_image(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT), interpolation=cv2.INTER_LINEAR) img = preprocess(img) img = np.expand_dims(img, axis=0) logger.debug(f'input image shape: {img.shape}') # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) output = predict(GMM_net, TOM_net, img, agnostic) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') if i != 0: total_time = total_time + (end - start) logger.info( f'\taverage time {total_time / (args.benchmark_count - 1)} ms') else: output = predict(GMM_net, TOM_net, img, agnostic) tryon, warped_cloth = output tryon = post_processing(tryon[0]) warped_cloth = post_processing(warped_cloth[0]) savepath = get_savepath(args.savepath, image_path, ext='.png') logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, tryon) savepath_warp = '%s-warp-cloth%s' % os.path.splitext(savepath) logger.info(f'saved at : {savepath_warp}') cv2.imwrite(savepath_warp, warped_cloth) logger.info('Script finished successfully.')
def recognize_from_image(filename, det_model, rec_model, ga_model): # prepare input data img = load_image(filename) print(f'input image shape: {img.shape}') # load identities ident_names, ident_feats = load_identities(rec_model) img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) # inference print('Start inference...') if args.benchmark: print('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) faces = predict(img, det_model, rec_model, ga_model) end = int(round(time.time() * 1000)) print(f'\tailia processing time {end - start} ms') else: faces = predict(img, det_model, rec_model, ga_model) faces = face_identification(faces, ident_feats) # plot result res_img = draw_detection(img, faces, ident_names) # plot result cv2.imwrite(args.savepath, res_img) print('Script finished successfully.')
def recognize_from_image(filename): # load input image img = load_image(filename) print(f'input image shape: {img.shape}') img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') detector = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=env_id) print('Start inference...') if args.benchmark: print('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) objs = detect_objects(img, detector) end = int(round(time.time() * 1000)) print(f'\tailia processing time {end - start} ms') else: objs = detect_objects(img, detector) # show image for obj in objs: dbface_utils.drawbbox(img, obj) cv2.imwrite(args.savepath, img) print('Script finished successfully.')
def recognize_from_image(filename, net, my_resnet): # prepare input data img = load_image(filename) logger.debug(f'input image shape: {img.shape}') img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) sents = predict(img, net, my_resnet) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: sents = predict(img, net, my_resnet) logger.info('### Caption ### ') logger.info(sents[0]) # plot result # cv2.imwrite(args.savepath, res_img) logger.info('Script finished successfully.')
def recognize_from_image(net): # input image loop for image_path in args.input: logger.info(image_path) img = load_image(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time_estimation = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) prob = predict(net, img) end = int(round(time.time() * 1000)) estimation_time = (end - start) # Logging logger.info( f'\tailia processing estimation time {estimation_time} ms') if i != 0: total_time_estimation = total_time_estimation + estimation_time logger.info( f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms' ) else: prob = predict(net, img) # show result print_results([prob], imagenet_classes) logger.info('Script finished successfully.')
def recognize_from_image(filename, detector): # prepare input data img_0 = load_image(filename) logger.debug(f'input image shape: {img_0.shape}') img = cv2.cvtColor(img_0, cv2.COLOR_BGRA2BGR) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) parsing_result = detect_objects(img, detector) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: parsing_result = detect_objects(img, detector) output_img = Image.fromarray(np.asarray(parsing_result, dtype=np.uint8)) palette = get_palette(len(CATEGORY)) output_img.putpalette(palette) savepath = get_savepath(args.savepath, filename) logger.info(f'saved at : {savepath}') output_img.save(savepath)
def recognize_from_image(filename, net): # prepare input data img = load_image(filename) logger.debug(f'input image shape: {img.shape}') img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) out_mask = predict(img, net) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: out_mask = predict(img, net) if not args.orig_size: img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT)) res_img = np.ones(img.shape, np.uint8) * 255 res_img[out_mask] = img[out_mask] # plot result savepath = get_savepath(args.savepath, filename) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) logger.info('Script finished successfully.')
def recognize_from_image(detector): # prepare input data org_img = load_image(args.input) print(f'input image shape: {org_img.shape}') img = cv2.cvtColor(org_img, cv2.COLOR_BGRA2RGB) img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT)) img = np.transpose(img, [2, 0, 1]) img = img.astype(np.float32) / 255 img = np.expand_dims(img, 0) # inference print('Start inference...') if args.benchmark: print('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) output = detector.predict([img]) end = int(round(time.time() * 1000)) print(f'\tailia processing time {end - start} ms') else: output = detector.predict([img]) detect_object = post_processing(img, THRESHOLD, IOU, output) # plot result res_img = plot_results(detect_object[0], org_img, COCO_CATEGORY) # plot result cv2.imwrite(args.savepath, res_img) print('Script finished successfully.')
def recognize_from_image(filename, detector): # load input image img = load_image(filename) img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) print('Start inference...') if args.benchmark: print('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) boxes, scores, cls_inds = detect_objects(img, detector) end = int(round(time.time() * 1000)) print(f'\tailia processing time {end - start} ms') else: boxes, scores, cls_inds = detect_objects(img, detector) try: print('\n'.join([ 'pos:{}, ids:{}, score:{:.3f}'.format( '(%.1f,%.1f,%.1f,%.1f)' % (box[0], box[1], box[2], box[3]), COCO_CATEGORY[int(obj_cls)], score) for box, obj_cls, score in zip(boxes, cls_inds, scores) ])) except: # FIXME: do not use base 'except' pass # show image im2show = draw_detection(img, boxes, scores, cls_inds) cv2.imwrite(args.savepath, im2show) print('Script finished successfully.')
def recognize_from_image(): # prepare input data img = load_image(args.input) print(f'input image shape: {img.shape}') # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, len(COCO_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_S_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV2, env_id=env_id ) detector.set_anchors(ANCHORS) # compute execution time for i in range(5): start = int(round(time.time() * 1000)) detector.compute(img, THRESHOLD, IOU) end = int(round(time.time() * 1000)) print(f'ailia processing time {end - start} ms') # plot result res_img = plot_results(detector, img, COCO_CATEGORY) cv2.imwrite(args.savepath, res_img) print('Script finished successfully.')
def recognize_from_image(net, params): category = params['category'] # input image loop for image_path in args.input: logger.info(image_path) # prepare input data img = load_image(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) pixel_labels = detect_objects(img, net, params['img_size']) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: pixel_labels = detect_objects(img, net, params['img_size']) output_img = Image.fromarray(np.asarray(pixel_labels, dtype=np.uint8)) # palette = get_palette(len(category)) palette = list(itertools.chain.from_iterable(category.values())) output_img.putpalette(palette) savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') output_img.save(savepath)
def recognize_from_image(filename, net): # prepare input data img = load_image(filename) logger.debug(f'input image shape: {img.shape}') img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) keypoints = predict(img, net) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: keypoints = predict(img, net) """ plot result """ res_img = draw_keypoints(img, keypoints) # cv2.imwrite(args.savepath, res_img) savepath = get_savepath(args.savepath, filename) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img)
def recognize_from_image(): # prepare input data img = load_image(args.input) print(f'input image shape: {img.shape}') # net initialize detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, len(COCO_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV3, env_id=args.env_id, ) if args.detection_width != DETECTION_SIZE or args.detection_height != DETECTION_SIZE: detector.set_input_shape(args.detection_width, args.detection_height) # inference print('Start inference...') if args.benchmark: print('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) detector.compute(img, args.threshold, args.iou) end = int(round(time.time() * 1000)) print(f'\tailia processing time {end - start} ms') else: detector.compute(img, args.threshold, args.iou) # plot result res_img = plot_results(detector, img, COCO_CATEGORY) cv2.imwrite(args.savepath, res_img) print('Script finished successfully.')
def process_embeds(gallery, gallery_imgs, gallery_embeds, model): logger.info('Exploring the gallery... (it may take a while)') modified = False # If some images have been removed from the gallery if len(gallery_imgs) <= len(gallery_embeds): removed_keys = set(gallery_embeds) - set(gallery_imgs) for key in removed_keys: modified = True del gallery_embeds[key] # If some images have been added or replaced from the gallery if len(gallery_imgs) >= len(gallery_embeds): added_keys = set(gallery_imgs) - set(gallery_embeds) for key in added_keys: modified = True # prepare input data img = load_image(os.path.join(gallery, key)) img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) img = preprocess(img) # inference embed = model.predict(img) gallery_embeds[key] = embed if len(gallery_embeds)+1 % 500 == 0: print(f"{len(gallery_embeds)}/{len(gallery_imgs)}") # Saves the new embeds dict if modified if modified: folder = os.path.join(gallery, 'gallery_embeds.pkl') with open(folder, 'wb') as file: pickle.dump(gallery_embeds, file) logger.info(f'Gallery embeds saved at : {folder}')
def recognize_from_image(filename, detector, pp_net): # prepare input data img_0 = load_image(filename) logger.debug(f'input image shape: {img_0.shape}') img = cv2.cvtColor(img_0, cv2.COLOR_BGRA2RGB) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) detect_object, seg_masks = detect_objects(img, detector, pp_net) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: detect_object, seg_masks = detect_objects(img, detector, pp_net) # plot result res_img = plot_results(detect_object, img_0, CATEGORY, segm_masks=seg_masks) savepath = get_savepath(args.savepath, filename) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img)
def recognize_from_image(filename, net): # prepare input data img = load_image(filename) #logger.info(f'input image shape: {img.shape}') img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) img = preprocess(img) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) preds_ailia = net.predict(img) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: preds_ailia = net.predict(img) #logger.info(f'output shape: {preds_ailia.shape}') search_gallery(net, preds_ailia, filename) savepath = get_savepath(args.savepath, filename) plt.savefig(savepath, bbox_inches='tight') logger.info(f'saved at : {savepath}')
def recognize_from_image(net): # input image loop for image_path in args.input: logger.info(image_path) # prepare input data img = load_image(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time_estimation = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) out_img = predict(net, img) end = int(round(time.time() * 1000)) estimation_time = (end - start) # Loggin logger.info(f'\tailia processing estimation time {estimation_time} ms') if i != 0: total_time_estimation = total_time_estimation + estimation_time logger.info(f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms') else: out_img = predict(net, img) # plot result savepath = get_savepath(args.savepath, image_path, ext='.png') logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, out_img) logger.info('Script finished successfully.')
def recognize_from_image(filenames, net): for filename in filenames: # prepare input data img = load_image(filename) logger.info(f'input image shape: {img.shape}') img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) bboxes = predict(img, net) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: bboxes = predict(img, net) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) res_img = draw_bbox(img, bboxes) # plot result savepath = get_savepath(args.savepath, filename) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) logger.info('Script finished successfully.')
def recognize_from_image(): # prepare input data img = load_image(args.input) print(f'input image shape: {img.shape}') # net initialize env_id = ailia.get_gpu_environment_id() print(f'env_id: {env_id}') detector = ailia.Detector(MODEL_PATH, WEIGHT_PATH, len(FACE_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV3, env_id=env_id) # inference print('Start inference...') if args.benchmark: print('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) detector.compute(img, THRESHOLD, IOU) end = int(round(time.time() * 1000)) print(f'\tailia processing time {end - start} ms') else: detector.compute(img, THRESHOLD, IOU) # plot result res_img = plot_results(detector, img, FACE_CATEGORY) cv2.imwrite(args.savepath, res_img) print('Script finished successfully.')
def recognize_from_video(net, face_net): capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format f_h = f_w = IMAGE_SIZE if args.savepath != SAVE_IMAGE_PATH: logger.warning( 'currently, video results cannot be output correctly...') writer = webcamera_utils.get_writer(args.savepath, f_h, f_w * 2) else: writer = None # create output buffer res_img = np.ones((f_h, f_w * 2, 3)) # get style image img_B = load_image(args.image_makeup) img_B = cv2.cvtColor(img_B, cv2.COLOR_BGRA2RGB) img_B_style = cv2.cvtColor(cv2.resize(img_B, (f_w // 4, f_h // 4)), cv2.COLOR_RGB2BGR) img_B, _ = preprocess(img_B) while True: ret, frame = capture.read() if cv2.waitKey(1) & 0xFF == ord('q'): break if not ret: continue # face detect img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img_A, (y, x) = face_detect(img, face_net) if not (img_A is None): res_img[:, 0:f_w, :] = cv2.cvtColor(cv2.resize(img_A, (f_w, f_h)), cv2.COLOR_RGB2BGR) res_img[f_h // 4 * 3:f_h, 0:f_w // 4, :] = img_B_style img_A, scale = preprocess(img_A) output = net.predict({'img_A': img_A, 'img_B': img_B}) fake_A, fake_B = output res_img[:, f_w:f_w * 2, :] = postprocess(fake_A[0]) # save results res_img = res_img.astype(np.uint8) if writer is not None: writer.write(res_img) # show cv2.imshow('frame', res_img) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() print('Script finished successfully.')
def recognize_from_image(): # net initialize detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, len(COCO_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV3, env_id=args.env_id, ) if args.detection_width != DETECTION_SIZE or args.detection_height != DETECTION_SIZE: detector.set_input_shape( args.detection_width, args.detection_height ) if args.profile: detector.set_profile_mode(True) # input image loop for image_path in args.input: # prepare input data logger.info(image_path) img = load_image(image_path) logger.debug(f'input image shape: {img.shape}') # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) detector.compute(img, args.threshold, args.iou) end = int(round(time.time() * 1000)) if i != 0: total_time = total_time + (end - start) logger.info(f'\tailia processing time {end - start} ms') logger.info(f'\taverage time {total_time / (args.benchmark_count-1)} ms') else: detector.compute(img, args.threshold, args.iou) # plot result res_img = plot_results(detector, img, COCO_CATEGORY) savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) # write prediction if args.write_prediction: pred_file = '%s.txt' % savepath.rsplit('.', 1)[0] write_predictions(pred_file, detector, img, COCO_CATEGORY) if args.profile: print(detector.get_summary()) logger.info('Script finished successfully.')
def bgr_image(shape=None): file_path = args.bgr_image img = load_image(file_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) img = preprocess(img, shape) return img
def recognize_from_image(filename, dict_net): # input image loop for image_path in args.input: # prepare input data logger.info(image_path) img = load_image(image_path) logger.info(f'input image shape: {img.shape}') img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) json_file = '.'.join([image_path.rsplit('.', 1)[0], 'json']) if not os.path.exists(json_file): raise FileNotFoundError('%s not exists' % json_file) with open(json_file) as f: options = json.load(f) points = options["points"] for _ in range(len(points)): points[_][1] = 1 - points[_][1] options["method"] = options.get("method", "colorization") options["alpha"] = float(options.get("alpha", 0.0)) if options.get("hasReference", False): ref_file = options.get("reference", "style.jpg") if not os.path.exists(ref_file): raise FileNotFoundError('%s not exists' % ref_file) reference = cv2.imread(ref_file) scale = max(reference.shape[:2]) / 256 if scale > 1.0: reference = cv2.resize(reference, (int(reference.shape[1] / scale), int(reference.shape[0] / scale))) options["reference"] = s_enhance(reference) else: options["reference"] = None options["line"] = options.get('line', False) options["lineColor"] = np.array(options.get('lineColor', [0, 0, 0])) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') for i in range(5): start = int(round(time.time() * 1000)) result = predict(img, dict_net, options) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') else: result = predict(img, dict_net, options) # plot result savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, result) logger.info('Script finished successfully.')
def recognize_from_image(net): img_B = load_image(args.image_makeup) img_B = cv2.cvtColor(img_B, cv2.COLOR_BGRA2RGB) img_B, _ = preprocess(img_B) # input image loop for image_path in args.input: logger.info(image_path) # prepare grand truth img_A = load_image(image_path) img_A = cv2.cvtColor(img_A, cv2.COLOR_BGRA2RGB) img_A, _ = preprocess(img_A) logger.debug(f'input image shape: {img_A.shape}') # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) output = net.predict({'img_A': img_A, 'img_B': img_B}) end = int(round(time.time() * 1000)) logger.info(f'\tailia processing time {end - start} ms') if i != 0: total_time = total_time + (end - start) logger.info( f'\taverage time {total_time / (args.benchmark_count - 1)} ms') else: output = net.predict({'img_A': img_A, 'img_B': img_B}) fake_A, fake_B = output output = np.concatenate([img_A[0], img_B[0], fake_A[0], fake_B[0]], axis=2) res_img = postprocess(output) savepath = get_savepath(args.savepath, image_path, ext='.png') logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) logger.info('Script finished successfully.')
def recognize_from_video(GMM_net, TOM_net, det_net, pose_net, seg_net): capture = webcamera_utils.get_capture(args.video) # create video writer if savepath is specified as video format if args.savepath != SAVE_IMAGE_PATH: f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) writer = webcamera_utils.get_writer(args.savepath, f_h, f_w) else: writer = None # prepare cloth image image_path = args_input img = load_image(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB) img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT), interpolation=cv2.INTER_LINEAR) img = preprocess(img) cloth_img = np.expand_dims(img, axis=0) dummy = np.zeros(IMAGE_HEIGHT * IMAGE_WIDTH).reshape( IMAGE_HEIGHT, IMAGE_WIDTH) while (True): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret: break # inference img = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA) img, offset, scale = human_detect(det_net, img) if offset == (0, 0): # human is not detected cv2.imshow('frame', dummy) continue agnostic = cloth_agnostic(pose_net, seg_net, img) agnostic = np.expand_dims(agnostic, axis=0) output = predict(GMM_net, TOM_net, cloth_img, agnostic) tryon, _ = output tryon = post_processing(tryon[0]) cv2.imshow('frame', tryon) # save results if writer is not None: writer.write(frame) capture.release() cv2.destroyAllWindows() if writer is not None: writer.release() logger.info('Script finished successfully.')
def recognize_from_image(net): # Load images inputs = args.input n_input = len(inputs) if n_input == 1 and args.input2: inputs.extend([args.input2]) if len(inputs) < 2: logger.error("Specified input must be at least two or more images") sys.exit(-1) for no, image_paths in enumerate(zip(inputs, inputs[1:])): logger.info(image_paths) # prepare input data images = [load_image(p) for p in image_paths] img1, img2 = [cv2.cvtColor(im, cv2.COLOR_BGRA2BGR) for im in images] # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time_estimation = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) out_img = predict(net, img1, img2) end = int(round(time.time() * 1000)) estimation_time = (end - start) # Loggin logger.info( f'\tailia processing estimation time {estimation_time} ms') if i != 0: total_time_estimation = total_time_estimation + estimation_time logger.info( f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms' ) else: out_img = predict(net, img1, img2) nm_ext = os.path.splitext(SAVE_IMAGE_PATH) save_file = "%s_%s%s" % (nm_ext[0], no, nm_ext[1]) save_path = get_savepath(args.savepath, save_file, post_fix='', ext='.png') logger.info(f'saved at : {save_path}') cv2.imwrite(save_path, out_img) logger.info('Script finished successfully.')
def recognize_from_image(): # net initialize detector = ailia.Detector( MODEL_PATH, WEIGHT_PATH, len(COCO_CATEGORY), format=ailia.NETWORK_IMAGE_FORMAT_RGB, channel=ailia.NETWORK_IMAGE_CHANNEL_FIRST, range=ailia.NETWORK_IMAGE_RANGE_U_FP32, algorithm=ailia.DETECTOR_ALGORITHM_YOLOV3, env_id=args.env_id, ) pose = ailia.Net(POSE_MODEL_PATH, POSE_WEIGHT_PATH, env_id=args.env_id) # input image loop for image_path in args.input: # prepare input data logger.info(image_path) img = load_image(image_path) logger.debug(f'input image shape: {img.shape}') # inference logger.info('Start inference...') detector.compute(img, THRESHOLD, IOU) # pose estimation if args.benchmark: logger.info('BENCHMARK mode') total_time = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) pose_detections = pose_estimation(detector, pose, img) end = int(round(time.time() * 1000)) logger.info( f'\tailia processing detection time {end - start} ms') if i != 0: total_time = total_time + (end - start) logger.info( f'\taverage detection time {total_time / (args.benchmark_count-1)} ms' ) else: pose_detections = pose_estimation(detector, pose, img) # plot result res_img = plot_results(detector, pose, img, COCO_CATEGORY, pose_detections) savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, res_img) logger.info('Script finished successfully.')
def recognize_from_image(net, detector): # input image loop for image_path in args.input: # prepare input data logger.info(image_path) img = load_image(image_path) if detector: img = recognize_from_frame(net, detector, img) savepath = get_savepath(args.savepath, image_path) logger.info(f'saved at : {savepath}') cv2.imwrite(savepath, img) continue img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE)) img = np.expand_dims(img, axis=0) # 次元合せ # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time_estimation = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) output = net.predict([img]) end = int(round(time.time() * 1000)) estimation_time = (end - start) # Logging logger.info( f'\tailia processing estimation time {estimation_time} ms') if i != 0: total_time_estimation = total_time_estimation + estimation_time logger.info( f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms' ) else: output = net.predict([img]) out_typ, out_clr = output typ = TYPE_LIST[np.argmax(out_typ)] clr = COLOR_LIST[np.argmax(out_clr)] logger.info("- Type: %s" % typ) logger.info("- Color: %s" % clr) logger.info('Script finished successfully.')
def recognize_from_image(net_image, net_text): top_k = 5 text_inputs = args.text_inputs desc_file = args.desc_file if desc_file: with open(desc_file) as f: text_inputs = [x.strip() for x in f.readlines() if x.strip()] elif text_inputs is None: text_inputs = [f"a {c}" for c in ("human", "dog", "cat")] text_feature = predict_text_feature(net_text, text_inputs) # input image loop for image_path in args.input: logger.info(image_path) # prepare input data img = load_image(image_path) img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) # inference logger.info('Start inference...') if args.benchmark: logger.info('BENCHMARK mode') total_time_estimation = 0 for i in range(args.benchmark_count): start = int(round(time.time() * 1000)) pred = predict(net_image, img, text_feature) end = int(round(time.time() * 1000)) estimation_time = (end - start) # Loggin logger.info( f'\tailia processing estimation time {estimation_time} ms') if i != 0: total_time_estimation = total_time_estimation + estimation_time logger.info( f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms' ) else: pred = predict(net_image, img, text_feature) inds = np.argsort(-pred)[:top_k] logger.info("Top predictions:") for i in inds: logger.info(f"{text_inputs[i]:>16s}: {100 * pred[i]:.2f}%") logger.info('Script finished successfully.')