def single_image_object_counting(input_video, detection_graph, category_index, is_color_recognition_enabled, fps, width, height): total_passed_vehicle = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." width_heigh_taken = True height = 0 width = 0 with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') input_frame = cv2.imread(input_video) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_single_image_array(1,input_frame, 1, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=4) if(len(counting_mode) == 0): cv2.putText(input_frame, "...", (10, 35), font, 0.8, (0,255,255),2,cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(input_frame, counting_mode, (10, 35), font, 0.8, (0,255,255),2,cv2.FONT_HERSHEY_SIMPLEX) cv2.imshow('object counting',input_frame) cv2.waitKey(0) return counting_mode
def detectSingleImage(self, input_video, detection_graph, category_index, is_color_recognition_enabled): counting_mode = "..." if "http" in input_video: img = cv2.VideoCapture(input_video) if (img.isOpened()): ret, input_frame = img.read() else: input_frame = cv2.imread(input_video) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = self.sess.run([ self.detection_boxes, self.detection_scores, self.detection_classes, self.num_detections ], feed_dict={self.image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_single_image_array( 1, input_frame, 1, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=4) if (len(counting_mode) == 0): cv2.putText(input_frame, "...", (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(input_frame, counting_mode, (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) status = cv2.imwrite(str(input_video), input_frame) # _,final_img = cv2.imencode(".jpeg", input_frame) while status == False: cv2.waitKey(100) return counting_mode
image_expanded = np.expand_dims(image, axis=0) # Perform the actual detection by running the model with the image as input (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_expanded}) # Draw the results of the detection (aka 'visulaize the results') counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_single_image_array( 1, image, 1, 0, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, min_score_thresh=0.3, line_thickness=8) boxs = np.squeeze(boxes) score = np.squeeze(scores) pre_class = counting_mode.split(':') # pre_class = pre_class.replace("'","") font = cv2.FONT_HERSHEY_SIMPLEX if (len(counting_mode) == 0): cv2.putText(image, "...", (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(image, counting_mode, (10, 35), font, 0.8, (0, 255, 255), 2,
def single_image_object_counting(input_video, detection_graph, category_index, is_color_recognition_enabled): total_passed_vehicle = 0 speed = "waiting..." direction = "waiting..." size = "waiting..." color = "waiting..." counting_mode = "..." with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name( 'detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') if "http" in input_video: img = cv2.VideoCapture(input_video) if (img.isOpened()): ret, input_frame = img.read() elif type(input_video) == numpy.ndarray: input_frame = cv2.imdecode(input_video, cv2.IMREAD_COLOR) else: input_frame = cv2.imread(input_video) print("CHRCKRKHRNCJCDZ") print(input_video) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(input_frame, axis=0) # Actual detection. (boxes, scores, classes, num) = sess.run([ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: image_np_expanded}) # insert information text to video frame font = cv2.FONT_HERSHEY_SIMPLEX # Visualization of the results of a detection. counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_single_image_array( 1, input_frame, 1, is_color_recognition_enabled, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=4) if (len(counting_mode) == 0): cv2.putText(input_frame, "...", (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(input_frame, counting_mode, (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) #cv2.imshow('tensorflow_object counting_api',input_frame) #cv2.waitKey(0) # status = cv2.imwrite(str(input_video),input_frame) _, final_img = cv2.imencode(".jpeg", input_frame) # while status == False : # cv2.waitKey(100) return counting_mode, final_img
def food_detection(): MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09' # object PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME, 'food_detection', 'frozen_inference_graph.pb') # Label PATH_TO_LABELS = os.path.join(CWD_PATH, 'data', 'food_detection', 'labelmap.pbtxt') # Path to image take_photo() NUM_CLASSES = 1 label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) # Load the Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.Session(graph=detection_graph) # Input tensor is the image image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') # Number of objects detected num_detections = detection_graph.get_tensor_by_name('num_detections:0') image = cv2.imread(PATH_TO_IMAGE) # image_dpi = Image.open(PATH_TO_IMAGE) image_expanded = np.expand_dims(image, axis=0) (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_expanded}) counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_single_image_array( 1, image, 1, 0, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, min_score_thresh=0.3, line_thickness=8) pre_food_class = counting_mode.split(':') sess.close() tf.reset_default_graph() cv2.destroyAllWindows() return len(pre_food_class)
def fish_detection(): #ทำนายปลา MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09' # IMAGE_NAME = 'image_fish.png' y_summax = 0 x_summax = 0 # object PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME, 'fish_detection', 'frozen_inference_graph.pb') # Label PATH_TO_LABELS = os.path.join(CWD_PATH, 'data', 'fish_detection', 'labelmap.pbtxt') # Path to image take_photo() NUM_CLASSES = 1 label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=NUM_CLASSES, use_display_name=True) category_index = label_map_util.create_category_index(categories) # Load the Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.Session(graph=detection_graph) # Input tensor is the image image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') # Number of objects detected num_detections = detection_graph.get_tensor_by_name('num_detections:0') image = cv2.imread(PATH_TO_IMAGE) image_dpi = Image.open(PATH_TO_IMAGE) image_expanded = np.expand_dims(image, axis=0) (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_expanded}) counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_single_image_array( 1, image, 1, 0, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, min_score_thresh=0.3, line_thickness=8) boxs = np.squeeze(boxes) score = np.squeeze(scores) pre_class = counting_mode.split(':') # print(pre_class) # pre_class = pre_class.replace("'","") font = cv2.FONT_HERSHEY_SIMPLEX if (len(counting_mode) == 0): cv2.putText(image, "...", (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) else: cv2.putText(image, counting_mode, (10, 35), font, 0.8, (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX) height, width, d = image.shape for i in range(0, len(score)): # print(boxes[[0], [i]]) if (score[i] >= 0.3): ymin, xmin, ymax, xmax = boxs[i] x1 = int(xmin * width) x2 = int(xmax * width) y1 = int(ymin * height) y2 = int(ymax * height) xmax, ymax = convert_pixel2inch(x2, y2, image_dpi) y_summax += ymax x_summax += xmax pre_class[2] = pre_class[2].strip() if int(pre_class[2]) > 1: x_avg_summax = x_summax / len(score) y_avg_summax = y_summax / len(score) print(str(xmax), str(ymax), str(pre_class[2])) sess.close() tf.reset_default_graph() cv2.destroyAllWindows() return xmax, ymax, int(pre_class[2])