def get_people_diff(cls, person1: 'ClassPeopleReId', person2: 'ClassPeopleReId'): # Comparing people between list diff_upper = ClassUtils.get_color_diff_rgb(person1.color_upper, person2.color_upper) diff_lower = ClassUtils.get_color_diff_rgb(person1.color_lower, person2.color_lower) diff_colors_1 = ClassUtils.get_color_diff_rgb(person1.color_upper, person1.color_lower) diff_colors_2 = ClassUtils.get_color_diff_rgb(person2.color_upper, person2.color_lower) diff_colors = math.fabs(diff_colors_1 - diff_colors_2) diff_k_means = ClassDescriptors.get_kmeans_diff( person1.hist_pose, person2.hist_pose) distance = cls.get_person_distance(person1, person2) return_data = { 'diffUpper': diff_upper, 'diffLower': diff_lower, 'diffColors': diff_colors, 'distance': distance, 'diffKMeans': diff_k_means } return return_data
def do_color_analysis(): print('Initializing main function') print('HSV analysis') pt1 = np.array([[[129, 120, 116]]], dtype=np.uint8) pt2 = np.array([[[99, 101, 101]]], dtype=np.uint8) diff = ClassUtils.get_color_diff_rgb([129, 120, 116], [99, 101, 101]) print(diff) hsv1 = cv2.cvtColor(pt1, cv2.COLOR_BGR2HSV) hsv2 = cv2.cvtColor(pt2, cv2.COLOR_BGR2HSV) hsv2[0, 0, 2] = 129 pt_con = cv2.cvtColor(hsv2, cv2.COLOR_HSV2BGR) print(pt1) print(pt2) print(pt_con) diff = ClassUtils.get_color_diff_rgb(to_rgb_arr(pt1), to_rgb_arr(pt_con)) print(diff) print('LAB analysis') lab1 = cv2.cvtColor(pt1, cv2.COLOR_BGR2LAB) lab2 = cv2.cvtColor(pt2, cv2.COLOR_BGR2LAB) lab2[0, 0, 0] = 129 pt_con = cv2.cvtColor(lab2, cv2.COLOR_LAB2BGR) print(pt1) print(pt2) print(pt_con) diff = ClassUtils.get_color_diff_rgb(to_rgb_arr(pt1), to_rgb_arr(pt_con)) print(diff)
def process_btf(): print('Processing BTF transformation') # Loading instances instance_pose = ClassOpenPose() image1, image2, pose1, pose2 = ClassDescriptors.load_images_comparision( instance_pose, min_score) hists1 = ClassDescriptors.get_color_histograms(pose1, min_score, image1, decode_img=False) hists2 = ClassDescriptors.get_color_histograms(pose2, min_score, image2, decode_img=False) # Plotting # plot_histograms(hists1) # Showing first images without transformation cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image2, decode_img=False) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image2))) print('Press any key to continue') cv2.waitKey(0) # Perform image transformation image_tr = ClassDescriptors.transform_image(image2, hists2, hists1) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image_tr, decode_img=False) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image_tr))) print('Press any key to continue') cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')
def main(): print('Initializing main function') color1 = [158, 145, 179] color2 = [70, 73, 93] delta_e = ClassUtils.get_color_diff_rgb(color1, color2) print('Color diff: {0}'.format(delta_e)) # Showing colors # In 100 x 100 window # Trying to compare colors visually cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) img_size = 100 image1 = np.zeros((img_size, img_size, 3), np.uint8) image2 = np.zeros((img_size, img_size, 3), np.uint8) # BGR format -> Generating image1[:, :] = (color1[2], color1[1], color1[0]) image2[:, :] = (color2[2], color2[1], color2[0]) # Showing image stacked img = np.hstack((image1, image2)) cv2.imshow('main_window', img) print('Press a key to continue') cv2.waitKey(0) print('Done!')
def process_lab_adj(): print('Process lab adjustment') # Loading instances instance_pose = ClassOpenPose() # Loading images image1, image2, pose1, pose2 = ClassDescriptors.load_images_comparision( instance_pose, min_score) # Performing color comparision upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image2, decode_img=False) # Performing custom comparison first diff_upper = ClassUtils.get_color_diff_rgb(upper1, upper2) diff_lower = ClassUtils.get_color_diff_rgb(lower1, lower2) print('Diff upper: {0}'.format(diff_upper)) print('Diff lower: {0}'.format(diff_lower)) # Performing lab conversion and equal L values diff_upper_lum = ClassUtils.get_color_diff_rgb_lum(upper1, upper2) diff_lower_lum = ClassUtils.get_color_diff_rgb_lum(lower1, lower2) print('Diff upper lum: {0}'.format(diff_upper_lum)) print('Diff lower lum: {0}'.format(diff_lower_lum)) # Showing images cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('main_window', np.hstack((image1, image2))) cv2.waitKey(0) print('Done!')
def compare_people_items(cls, person1: 'ClassPeopleReId', person2: 'ClassPeopleReId'): # Comparing people between list diff_upper = ClassUtils.get_color_diff_rgb(person1.color_upper, person2.color_upper) diff_lower = ClassUtils.get_color_diff_rgb(person1.color_lower, person2.color_lower) distance = cls.get_person_distance(person1, person2) # Color diff between 1 and 100 # Distance in cm # Normalize diffs between 0 and 50 - If greater, diff is one norm_upper = diff_upper if norm_upper > 50: norm_upper = 50 norm_upper /= 50 norm_lower = diff_lower if norm_lower > 50: norm_lower = 50 norm_lower /= 50 # Change distance for velocity delta_time = math.fabs( (person1.last_date - person2.last_date).total_seconds()) if delta_time == 0: print('Hello!') velocity = distance / delta_time if velocity > 200: velocity = 200 velocity /= 200 return norm_upper, norm_lower, velocity
def main(): print('Initializing main function') color1 = [199, 164, 112] color2 = [128, 112, 132] delta_e = ClassUtils.get_color_diff_rgb(color1, color2) print('Color diff: {0}'.format(delta_e)) # Showing colors # In 100 x 100 window # Trying to compare colors visually cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) img_size = 100 image1_1 = np.zeros((img_size, img_size, 3), np.uint8) image2_1 = np.zeros((img_size, img_size, 3), np.uint8) # BGR format -> Generating image1_1[:, :] = (color1[2], color1[1], color1[0]) image2_1[:, :] = (color2[2], color2[1], color2[0]) # Showing image stacked img = np.hstack((image1_1, image2_1)) result1 = ClassColorComparison.classify_color(color1) result2 = ClassColorComparison.classify_color(color2) image1_2 = np.zeros((img_size, img_size, 3), np.uint8) image2_2 = np.zeros((img_size, img_size, 3), np.uint8) image1_2[:, :] = (result1[1][2], result1[1][1], result1[1][0]) image2_2[:, :] = (result2[1][2], result2[1][1], result2[1][0]) img_aux = np.hstack((image1_2, image2_2)) img_result = np.vstack((img, img_aux)) cv2.imshow('main_window', img_result) print('Press a key to continue') cv2.waitKey(0) print('Done!')
def main(): print('Initializing main function') Tk().withdraw() # filename1 = '/home/mauricio/Pictures/Poses/temp/636550787632290048_419.jpg' # filename2 = '/home/mauricio/Pictures/Poses/walk_front/636550801813440000_424.jpg' filename1 = '/home/mauricio/Pictures/Poses/bend_left/636453039344460032_1.jpg' filename2 = '/home/mauricio/Pictures/Poses/left_walk/636550795366450048_420.jpg' print('Select first file') init_dir = '/home/mauricio/Pictures/Poses' options = {'initialdir': init_dir} filename1_tmp = askopenfilename(**options) if filename1_tmp: filename1 = filename1_tmp print('File selected: {0}'.format(filename1)) print('Select second file') filename2_tmp = askopenfilename(**options) if filename2_tmp: filename2 = filename2_tmp print('File selected: {0}'.format(filename2)) img1 = cv2.imread(filename1) img2 = cv2.imread(filename2) instance_pose = ClassOpenPose() vectors1, img_draw1 = instance_pose.recognize_image_tuple(img1) vectors2, img_draw2 = instance_pose.recognize_image_tuple(img2) if len(vectors1) != 1: raise Exception('Invalid len for vector 1') if len(vectors2) != 1: raise Exception('Invalid len for vector 2') person_vector1 = vectors1[0] person_vector2 = vectors2[0] min_percent = 0.05 if not ClassUtils.check_vector_integrity_pos(person_vector1, min_percent): raise Exception('Invalid integrity for vector 1') if not ClassUtils.check_vector_integrity_pos(person_vector2, min_percent): raise Exception('Invalid integrity for vector 2') colors1 = ClassDescriptors.process_colors(vectors1, min_percent, img1, decode_img=False) colors2 = ClassDescriptors.process_colors(vectors2, min_percent, img2, decode_img=False) upper1 = colors1[0][0] lower1 = colors1[1][0] color_diff1 = ClassUtils.get_color_diff_rgb(upper1, lower1) upper2 = colors2[0][0] lower2 = colors2[1][0] color_diff2 = ClassUtils.get_color_diff_rgb(upper2, lower2) diff_upper = ClassUtils.get_color_diff_rgb(upper1, upper2) diff_lower = ClassUtils.get_color_diff_rgb(lower1, lower2) diff_diff = math.fabs(color_diff1 - color_diff2) print('Diff upper: {0}'.format(diff_upper)) print('Diff lower: {0}'.format(diff_lower)) print('Diff diffs: {0}'.format(diff_diff)) cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('main_window', np.hstack((img_draw1, img_draw2))) cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')
def process_test_color_eq(): print('Processing color eq') # Loading instances instance_pose = ClassOpenPose() im1, im2, pose1, pose2 = ClassDescriptors.load_images_comparision( instance_pose, min_score) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, im1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, im2, decode_img=False) img_size = 100 image1 = np.zeros((img_size, img_size, 3), np.uint8) image2 = np.zeros((img_size, img_size, 3), np.uint8) image3 = np.zeros((img_size, img_size, 3), np.uint8) image4 = np.zeros((img_size, img_size, 3), np.uint8) # Generating elements in list cv2.namedWindow('aux_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow('aux_window', np.hstack((im1, im2))) # BGR format -> Generating image1[:, :] = (upper1[2], upper1[1], upper1[0]) image2[:, :] = (lower1[2], lower1[1], lower1[0]) image3[:, :] = (upper2[2], upper2[1], upper2[0]) image4[:, :] = (lower2[2], lower2[1], lower2[0]) # Performing custom comparison first diff_upper = ClassUtils.get_color_diff_rgb(upper1, upper2) diff_lower = ClassUtils.get_color_diff_rgb(lower1, lower2) print('Diff upper: {0}'.format(diff_upper)) print('Diff lower: {0}'.format(diff_lower)) cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE) cv2.imshow( 'main_window', np.hstack((np.vstack((image1, image2)), np.vstack((image3, image4))))) cv2.waitKey(0) # Performing lab conversion and equal L values diff_upper_lum = ClassUtils.get_color_diff_rgb_lum(upper1, upper2) diff_lower_lum = ClassUtils.get_color_diff_rgb_lum(lower1, lower2) # Transform point upper2_eq = ClassUtils.eq_lum_rgb_colors(upper1, upper2) lower2_eq = ClassUtils.eq_lum_rgb_colors(lower1, lower2) print('Diff upper lum: {0}'.format(diff_upper_lum)) print('Diff lower lum: {0}'.format(diff_lower_lum)) image3[:, :] = (upper2_eq[2], upper2_eq[1], upper2_eq[0]) image4[:, :] = (lower2_eq[2], lower2_eq[1], lower2_eq[0]) # Showing again image cv2.imshow( 'main_window', np.hstack((np.vstack((image1, image2)), np.vstack((image3, image4))))) cv2.waitKey(0) # Destroying all windows cv2.destroyAllWindows() print('Done!')
def main(): Tk().withdraw() instance_pose = ClassOpenPose() print('Initializing main function') list_files1 = os.listdir(base1_folder) hists_1 = read_hists(base1_folder, list_files1) list_files2 = os.listdir(base2_folder) hists_2 = read_hists(base2_folder, list_files2) cum_hists_1 = ClassDescriptors.get_cumulative_hists(hists_1) cum_hists_2 = ClassDescriptors.get_cumulative_hists(hists_2) # Processing img filename1 = '/home/mauricio/Pictures/Poses/bend_left/636453039344460032_1.jpg' filename2 = '/home/mauricio/Pictures/Poses/left_walk/636550795366450048_420.jpg' init_dir = '/home/mauricio/Pictures' options = {'initialdir': init_dir} filename1 = askopenfilename(**options) if not filename1: filename1 = '/home/mauricio/Pictures/2_1.jpg' ext1 = os.path.splitext(filename1)[1] if ext1 != '.jpg' and ext1 != '.jpeg': raise Exception('Extension1 is not jpg or jpeg') print('Loading filename 2') filename2 = askopenfilename(**options) if not filename2: filename2 = '/home/mauricio/Pictures/2_2.jpg' ext2 = os.path.splitext(filename2)[1] if ext2 != '.jpg' and ext2 != '.jpeg': raise Exception('Extension2 is not jpg or jpeg') image1 = cv2.imread(filename1) if image1 is None: raise Exception('Invalid image in filename {0}'.format(filename1)) image2 = cv2.imread(filename2) if image2 is None: raise Exception('Invalid image in filename {0}'.format(filename2)) is_json = True new_file_1 = filename1.replace('.jpeg', '.json') new_file_1 = new_file_1.replace('.jpg', '.json') new_file_2 = filename2.replace('.jpeg', '.json') new_file_2 = new_file_2.replace('.jpg', '.json') if not os.path.exists(new_file_1): print('File not found: {0}'.format(new_file_1)) is_json = False if not os.path.exists(new_file_2): print('File not found: {0}'.format(new_file_2)) is_json = False if not is_json: poses1 = instance_pose.recognize_image(image1) poses2 = instance_pose.recognize_image(image2) if len(poses1) != 1: raise Exception('Invalid len for pose1: {0}'.format(len(poses1))) if len(poses2) != 1: raise Exception('Invalid len for pose2: {0}'.format(len(poses2))) if not ClassUtils.check_vector_integrity_pos(poses1[0], min_score): raise Exception('Pose 1 not valid') if not ClassUtils.check_vector_integrity_pos(poses2[0], min_score): raise Exception('Pose 2 not valid') pose1 = poses1[0] pose2 = poses2[0] else: with open(new_file_1, 'r') as f: obj_json1 = json.loads(f.read()) with open(new_file_2, 'r') as f: obj_json2 = json.loads(f.read()) pose1 = obj_json1['vector'] pose2 = obj_json2['vector'] if not ClassUtils.check_vector_integrity_pos(pose1, min_score): raise Exception('Pose 1 not valid') if not ClassUtils.check_vector_integrity_pos(pose2, min_score): raise Exception('Pose 2 not valid') # Plotting # plot_histograms(hists1) # Showing first images without transformation cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image2, decode_img=False) print('Upper1 {0} - Lower1 {0}'.format(upper1, lower1)) print('Upper2 {0} - Lower2 {0}'.format(upper2, lower2)) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image2))) print('Press any key to continue') cv2.waitKey(0) # Perform image transformation image_tr = ClassDescriptors.transform_image(image2, cum_hists_2, cum_hists_1) upper1, lower1 = ClassDescriptors.process_colors_person(pose1, min_score, image1, decode_img=False) upper2, lower2 = ClassDescriptors.process_colors_person(pose2, min_score, image_tr, decode_img=False) print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2))) print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2))) cv2.imshow('main_window', np.hstack((image1, image_tr))) print('Press any key to continue') cv2.waitKey(0) cv2.destroyAllWindows() print('Done!')