def generate(): propcountarr = [] with open(config.prop_json) as f: jsonprop = json.load(f) rgb_files = [f for f in listdir(rgb_folder) if isfile(join(rgb_folder, f))] for i in range(0, len(rgb_files)): #print(rgb_files[i]) image_name = rgb_files[i] #jsonprop["proposals"][i]["file"] image_name_png = image_name.replace("jpg", "png") count = 0 for j in range(0, len(jsonprop)): #[i]["objects"])): if (jsonprop[j]["file"] == image_name_png): propcount = 0 for k in range(0, len(jsonprop[j]["objects"])): propcount = propcount + 1 x = jsonprop[j]["objects"][k]["x"] y = jsonprop[j]["objects"][k]["y"] z = jsonprop[j]["objects"][k]["z"] xmiw, ymiw, xmaw, ymaw = core_functions.get_bounding_box_WASSIMEA( x, y, z) prop_object = [xmiw, ymiw, xmaw, ymaw, y, x] headpoint = [prop_object[4], prop_object[5]] image, headpoint_new = core_functions.get_neg_roi( rgb_folder + image_name, prop_object, headpoint) if config.mod == True: image = mod.get_channels(image, headpoint_new) cv2.normalize(image, image, 0, 255, cv2.NORM_MINMAX) width, height = image.shape[1], image.shape[0] if (width > 30 and height > 30): #c1,c2,c3,mod = core_functions.get_channels(image, headpoint_new) cv2.imwrite( out_folder + image_name.replace(".jpg", "_id_") + str(count) + ".jpg", image) count = count + 1 zabre = np.mean(propcountarr) pingit = 1
def process_image(filename, jsongt, jsonprop): #png_image = cv2.imread(filename, -1) #counter = 0 filename_only_jpg = os.path.basename(filename).replace(".png", ".jpg") filename_only_png = filename_only_jpg.replace("jpg", "png") #print(filename_only_jpg) gtarray = common_functions.get_gt_annotations(filename_only_jpg, jsongt) proparray = common_functions.get_proposals(filename_only_png, jsonprop) flag_array = np.zeros(len(proparray)) #if "videos_holland_orbbec_2018-02-21_CAM2_1519231983220" in filename: # im = cv2.imread(filename) # for gt in gtarray: # cv2.rectangle(im,(gt[0], gt[1]), (gt[2], gt[3]),(0,255,0), 1) # for prop in proparray: # cv2.rectangle(im,(prop[0], prop[1]), (prop[2], prop[3]),(0,0,255), 1) # # cv2.imshow("im",im) # cv2.waitKey() for gt_object in gtarray: id = gt_object[6] if 1 == 1: #common_functions.check_box_boundaries(gt_object[0],gt_object[1],gt_object[2],gt_object[3]): list_valid_proposals = [] list_valid_proposals_ious = [] for prop_object in proparray: #if(flag_array[proparray.index(prop_object)] != 1): iou = core_functions.bb_intersection_over_union( gt_object, prop_object) #if(common_functions.check_if_box_is_contained(gt_object,prop_object)): if (iou > 0.75): list_valid_proposals.append(prop_object) list_valid_proposals_ious.append(iou) if (len(list_valid_proposals_ious) > 0): #list_acceptable_proposals = [] #idx = np.argsort(list_valid_proposals_ious) # #list_valid_proposals_ious = np.array(list_valid_proposals_ious)[idx] #list_valid_proposals = np.array(list_valid_proposals)[idx] #list_valid_proposals_ious = list_valid_proposals_ious.tolist() #list_valid_proposals = list_valid_proposals.tolist() for i in range(0, len(list_valid_proposals)): #highest_iou_index = np.argmax(list_valid_proposals_ious) #prop_object = list_valid_proposals[highest_iou_index] #flag_array[proparray.index(list_valid_proposals[i])] = 1 image, head_new, right_shoulder_new, left_shoulder_new, headpoint_new = core_functions.get_new_roi_with_annotations( filename, list_valid_proposals[i], gt_object) if config.mod == True: if ("video_fifth_orbbec_2018-01-19_lunch_CAM1_1516381409554.png" in filename): x = 1 #image = mod.get_channels(image,headpoint_new)#.astype('uint8') image = mod.get_channels(image, headpoint_new) #roi1copy = roi1.copy() #b,g,r = cv2.split(image) #b = cv2.normalize(b, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX).astype('uint8') #g = g.astype('uint8') #r = cv2.normalize(r, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX).astype('uint8') #b = (b/256).astype('uint8') #g = (g/256).astype('uint8') #r = (r/256).astype('uint8') #image = cv2.merge((b,g,r)).astype('uint8') #cv2.imshow("im",image) #cv2.waitKey() #cv2.normalize(image, image, 0, 255, cv2.NORM_MINMAX) #image = image.astype('uint8') if image is not None: encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 95] result, encimg = cv2.imencode('.jpg', image, encode_param) image = cv2.imdecode(encimg, 1) flag_array[proparray.index( list_valid_proposals[i])] = 1 width, height = image.shape[1], image.shape[0] if (width > 2 and height > 2): # and num_zeros < (width*height)/ 3): data['frames'].append({ 'file': filename_only_png.replace(".png", "_id_") + str(id) + "_index_" + str(i) + ".png", 'width': width, 'height': height, 'annotations': [{ 'label': 'Head', 'x': head_new[0], 'y': head_new[1], 'width': head_new[2] - head_new[0], 'height': head_new[3] - head_new[1] }, { 'label': 'Headpoint', 'x': headpoint_new[0], 'y': headpoint_new[1] }, { 'label': 'Right Shoulder', 'x': right_shoulder_new[1], 'y': right_shoulder_new[0] }, { 'label': 'Left Shoulder', 'x': left_shoulder_new[1], 'y': left_shoulder_new[0] }] }) cv2.imwrite( pos_out_folder + filename_only_png.replace(".png", "_id_") + str(id) + "_index_" + str(i) + ".png", image) for i in range(0, len(flag_array)): if flag_array[i] == 0: headpoint = [proparray[i][4], proparray[i][5]] image, headpoint_new = core_functions.get_neg_roi( filename, proparray[i], headpoint) if config.mod == True: image = mod.get_channels(image, headpoint_new) if image is not None: encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 95] result, encimg = cv2.imencode('.jpg', image, encode_param) image = cv2.imdecode(encimg, 1) cv2.imwrite( neg_out_folder + filename_only_png.replace(".png", "_id_") + str(i) + ".png", image)
def process_image(filename, jsongt, jsonprop): #png_image = cv2.imread(filename, -1) #counter = 0 filename_only_png = os.path.basename(filename) filename_only_jpg = filename_only_png.replace("png", "jpg") print(filename_only_png) gtarray = common_functions.get_gt_annotations(filename_only_jpg, jsongt) proparray = common_functions.get_proposals(filename_only_png, jsonprop) #for gt_object in gtarray: if 1 == 1: gt_object = gtarray[0] id = gt_object[6] if common_functions.check_box_boundaries(gt_object[0], gt_object[1], gt_object[2], gt_object[3]): for prop_object in proparray: iou = core_functions.bb_intersection_over_union( gt_object, prop_object) if (iou > 0.5): contained = True image, head_new, right_shoulder_new, left_shoulder_new, headpoint_new = core_functions.get_new_roi_with_annotations( filename, prop_object, gt_object) num_zeros = (image == 0).sum() width, height = image.shape[1], image.shape[0] if (width > 2 and height > 2 and num_zeros < (width * height) / 3): data['frames'].append({ 'file': filename_only_png.replace(".png", "_id_") + str(id) + ".jpg", 'width': width, 'height': height, 'annotations': [{ 'label': 'Head', 'x': head_new[0], 'y': head_new[1], 'width': head_new[2] - head_new[0], 'height': head_new[3] - head_new[1] }, { 'label': 'Headpoint', 'x': headpoint_new[0], 'y': headpoint_new[1] }, { 'label': 'Right Shoulder', 'x': right_shoulder_new[1], 'y': right_shoulder_new[0] }, { 'label': 'Left Shoulder', 'x': left_shoulder_new[1], 'y': left_shoulder_new[0] }] }) #startpy = time.clock() #mod = core_functions.get_channels(image, headpoint_new) #timepy = time.clock() - startpy startcy = time.clock() #image = np.ascontiguousarray(image) mod1 = mod.get_channels(image, headpoint_new) #cv2.imshow("x",mod1) #cv2.waitKey() timecy = time.clock() - startcy #cv2.rectangle(mod,(head_new[0],head_new[1]),(head_new[2] ,head_new[3]),(255,0,0),3) #cv2.circle(mod,(right_shoulder_new[0], right_shoulder_new[1]), 5, (0,255,0), -1) #cv2.circle(mod,(left_shoulder_new[0], left_shoulder_new[1]), 5, (0,255,0), -1) #cv2.imwrite(out_folder + filename.replace(".png","") + "/" + str(id) + "/c1.png",c1) #cv2.imwrite(out_folder + filename.replace(".png","") + "/" + str(id) + "/c2.jpg",c2) #cv2.imwrite(out_folder + filename.replace(".png","") + "/" + str(id) + "/c3.jpg",c3) #cv2.imwrite(out_folder + filename.replace(".png","") + "/" + str(id) + "/mod.jpg",mod) #cv2.imwrite("mod.jpg", mod) cv2.imwrite( "/home/wassimea/Desktop/chl_work/mod_scripts/modtest.jpg", mod1) cv2.imshow("ayre", mod1) cv2.waitKey() print(timecy) break
def evaluate_3c(index): config = singleton.configuration._instance.config data = {} data['results'] = [] tf.logging.set_verbosity(tf.logging.FATAL) logging.getLogger("tensorflow").setLevel(logging.ERROR) pingit = common_functions.Detector(index, config) images_folder = config.rgb_folder display_folder = config.display_folder images = [ f for f in listdir(images_folder) if isfile(join(images_folder, f)) ] jsonprop = config.jsonprop jsongt = config.jsongt iou_props_total = 0 iou_detections_total = 0 total_detections = 0 total_gt = 0 total_tp = 0 total_fp = 0 total_pckh_precision = 0 master_count = 0 #val_folder = "/Data2TB/chl_data/mod/val/pos_neg_png_48/" images = [ f for f in listdir(images_folder) if isfile(join(images_folder, f)) ] pos = 0 neg = 0 for image in images: img = cv2.imread(images_folder + image, -1) #img = cv2.imread(val_folder + "video_fifth_2018-04-23_CAM1_1524502510848_id_791_index_0.png") positive_props = [] detections_regressed = [] img = cv2.imread(images_folder + image, -1) #img = cv2.imread("/Data2TB/chl_data/mod/val/pos_png_48/video_fifth_2018-04-23_CAM1_1524502858816_id_831_index_0.png",-1) ##roi_detect = img.reshape(1, 48,48,3) ##roi_detect = (roi_detect.astype('float32') - 127.5)/128 ##roi_detect = roi_detect[...,::-1] proposals, gtobjects = common_functions.get_proposal_and_gt_boxes_mod( image, jsonprop, jsongt) display_image = cv2.imread(display_folder + image) # # count = 0 propcount = 0 for prop in proposals: total_detections += len(proposals) imgtemp = img.copy() #cv2.imshow("tmp",imgtemp) #cv2.waitKey() xmin = prop[0][0] ymin = prop[0][1] xmax = prop[0][2] ymax = prop[0][3] headpoint = prop[1] xmin, ymin, xmax, ymax = common_functions.refine_bounding_box( xmin, ymin, xmax, ymax) roi = imgtemp[ymin:ymax, xmin:xmax] x_roi = headpoint[0] - xmin y_roi = headpoint[1] - ymin headpoint_roi = [y_roi, x_roi] roi = mod.get_channels(roi, headpoint_roi) # #roi1copy = roi1.copy() encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 95] result, encimg = cv2.imencode('.jpg', roi, encode_param) roi = cv2.imdecode(encimg, 1) #cv2.imshow("WAS IT LOS", roi) #cv2.waitKey() count += 1 # #continue # #videos_holland_orbbec_2018-02-21_CAM2_1519232343120_id_66320_index_0.png width_original = roi.shape[1] width_diff = width_original - 48 # roi_detect = cv2.resize(roi, (48, 48)) roi_detect = roi_detect.reshape(1, 48, 48, 3) roi_detect = (roi_detect.astype('float32') - 127.5) / 128 roi_detect = roi_detect[..., ::-1] #cls_scores = common_functions.testpredict(roi_detect) cls_scores = pingit.predict(roi_detect)[0] #print(cls_scores) ind = np.argmax(cls_scores) if (ind == 1): zabri = 1 pos += 1 #cv2.imshow("pos", img) #cv2.waitKey() positive_props.append(prop) #bb,lm = common_functions.rescale_detections_to_full_image(bb,lm, prop) #detection = [bb,lm] #detections_regressed.append(detection) ious = eval_functions.get_iou_totals_per_image(gtobjects, positive_props, images_folder + image) ##shoulder_precisions = eval_functions.get_shoulder_precisions(gtobjects, detections_regressed) # iou_props_total += ious[0] iou_detections_total += ious[1] total_gt += ious[0] total_tp += ious[1] total_fp += ious[2] if (ious[0] != 0 and ious[1] != 0): master_count += 1 recall = total_tp / total_gt miss_rate = total_fp / (total_tp + total_fp) #av_pckh_precision = total_pckh_precision/(master_count) data['results'].append({ 'at': str(index), 'recall': recall, 'miss_rate': miss_rate }) with open( config.results_out + config.mtcnn_model + config.bbox_loss + ".json", 'a') as outfile: json.dump(data, outfile, indent=4) return recall, miss_rate
def evaluate_3c(pingit, images_folder): images_folder = "/Data2TB/correctly_registered/S12/test/depth/" images = [ f for f in listdir(images_folder) if isfile(join(images_folder, f)) ] jsonprop = "/Data2TB/correctly_registered/S12/test/output.json" jsongt = "/Data2TB/sample/annotations.json" list_thresholds = [ 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9 ] percentage = 0.5 list_precisions = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] list_recalls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] list_gt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_gt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_tn = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_tp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_fp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_outl = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_fn = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_pckh_precision = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] total_true_detections_p5 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] iou_props_total = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] iou_detections_total = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] master_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] av_iou_prop = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] av_iou_detection = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] recall = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] miss_rate = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] precision = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] av_pckh_precision = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for image in images: total_proposals = 0 #image = images[i] positive_props = [] detections_regressed = [] img = cv2.imread(images_folder + image, -1) proposals, gtobjects = common_functions.get_proposal_and_gt_boxes_mod( image, jsonprop, jsongt) count = 0 propcount = 0 list_positive_props = [[], [], [], [], [], [], [], [], [], [], [], [], [], []] list_detections_regressed = [[], [], [], [], [], [], [], [], [], [], [], [], [], []] for prop in proposals: total_proposals += 1 imgtemp = img.copy() #cv2.imshow("tmp",imgtemp) #cv2.waitKey() xmin = prop[0][0] ymin = prop[0][1] xmax = prop[0][2] ymax = prop[0][3] headpoint = prop[1] xmin, ymin, xmax, ymax = common_functions.refine_bounding_box( xmin, ymin, xmax, ymax) roi = imgtemp[ymin:ymax, xmin:xmax] x_roi = headpoint[0] - xmin y_roi = headpoint[1] - ymin headpoint_roi = [y_roi, x_roi] roi = mod.get_channels(roi, headpoint_roi) # #roi1copy = roi1.copy() encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 95] result, encimg = cv2.imencode('.jpg', roi, encode_param) roi = cv2.imdecode(encimg, 1) width_original = roi.shape[1] roi_detect = cv2.resize(roi, (48, 48)) roi_detect = roi_detect.reshape(1, 48, 48, 3) roi_detect = (roi_detect.astype('float32') - 127.5) / 128 roi_detect = roi_detect[..., ::-1] cls_scores = pingit.predict(roi_detect)[0] for i in range(len(list_thresholds)): if (cls_scores[0][1] >= list_thresholds[i]): list_positive_props[i].append(prop) #bbtmp,lmtmp = common_functions.rescale_detections_to_full_image(bb,lm, prop) #list_detections_regressed[i].append([bbtmp,lmtmp]) for i in range(len(list_thresholds)): results = get_iou_totals_per_image(gtobjects, list_positive_props[i], images_folder + image, percentage) iou_props_total[i] += results[0] iou_detections_total[i] += results[1] total_gt[i] += results[2] total_tp[i] += results[3] total_fp[i] += results[4] total_outl[i] += results[6] total_fn[i] += results[7] total_tn[ i] += total_proposals - results[3] - results[4] - results[6] total_pckh_precision[i] += results[5] total_true_detections_p5[i] += results[8] #total_precisions += ious[5] if (results[0] != 0): master_count[i] += 1 for i in range(len(list_thresholds)): av_iou_prop[i] = iou_props_total[i] / master_count[i] av_iou_detection[i] = iou_detections_total[i] / master_count[i] recall[i] = total_tp[i] / total_gt[i] miss_rate[i] = total_fp[i] / (total_fp[i] + total_tn[i]) precision[i] = total_tp[i] / (total_tp[i] + total_fp[i]) av_pckh_precision[i] = total_pckh_precision[i] / (master_count[i]) #recall[i] = total_tp[i] / total_gt[i] #precision[i] = total_tp[i] / (total_tp[i] + total_fp[i]) print("Threshold: ", round(list_thresholds[i], 2)) #print("Average prop IOU: " , round(av_iou_prop[i],2)) #print("Average detection IOU: " , round(av_iou_detection[i],2)) #print("PCKH: " , round(av_pckh_precision[i],2)) print("Recall: ", round(recall[i], 2)) print("Precision: ", round(precision[i], 2)) print("") print("") return av_iou_detection, recall, miss_rate, av_pckh_precision, av_iou_prop