Kasamuday commited on
Commit
0851b0f
1 Parent(s): 7a12f1d
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import os
3
+ import cv2
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from object_detection.utils import label_map_util
7
+ from object_detection.utils import visualization_utils as viz_utils
8
+ from object_detection.builders import model_builder
9
+ from object_detection.utils import config_util
10
+
11
+ app = Flask(__name__)
12
+
13
+ # Load model and label map
14
+ CUSTOM_MODEL_NAME = 'my_ssd_mobnet'
15
+ paths = {
16
+ 'CHECKPOINT_PATH': os.path.join('Tensorflow', 'workspace', 'models', CUSTOM_MODEL_NAME),
17
+ 'LABELMAP': os.path.join('Tensorflow', 'workspace', 'annotations', 'label_map.pbtxt')
18
+ }
19
+
20
+ configs = config_util.get_configs_from_pipeline_file(os.path.join(paths['CHECKPOINT_PATH'], 'pipeline.config'))
21
+ detection_model = model_builder.build(model_config=configs['model'], is_training=False)
22
+ ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
23
+ ckpt.restore(os.path.join(paths['CHECKPOINT_PATH'], 'ckpt-7')).expect_partial()
24
+ category_index = label_map_util.create_category_index_from_labelmap(paths['LABELMAP'])
25
+
26
+ # Define detection function
27
+ @tf.function
28
+ def detect_fn(image):
29
+ image, shapes = detection_model.preprocess(image)
30
+ prediction_dict = detection_model.predict(image, shapes)
31
+ detections = detection_model.postprocess(prediction_dict, shapes)
32
+ return detections
33
+
34
+ # Define route for object detection
35
+ @app.route('/detect', methods=['POST'])
36
+ def detect():
37
+ # Get image file from request
38
+ file = request.files['image']
39
+
40
+ # Read image and convert to numpy array
41
+ img = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
42
+ image_np = np.array(img)
43
+
44
+ # Perform object detection
45
+ input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
46
+ detections = detect_fn(input_tensor)
47
+
48
+ num_detections = int(detections.pop('num_detections'))
49
+ detections = {key: value[0, :num_detections].numpy() for key, value in detections.items()}
50
+ detections['num_detections'] = num_detections
51
+ detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
52
+
53
+ label_id_offset = 1
54
+ image_np_with_detections = image_np.copy()
55
+
56
+ viz_utils.visualize_boxes_and_labels_on_image_array(
57
+ image_np_with_detections,
58
+ detections['detection_boxes'],
59
+ detections['detection_classes'] + label_id_offset,
60
+ detections['detection_scores'],
61
+ category_index,
62
+ use_normalized_coordinates=True,
63
+ max_boxes_to_draw=10,
64
+ min_score_thresh=.4,
65
+ agnostic_mode=False
66
+ )
67
+
68
+ # Convert image back to byte stream
69
+ ret, buffer = cv2.imencode('.jpg', image_np_with_detections)
70
+ img_str = buffer.tobytes()
71
+
72
+ return img_str
73
+
74
+ # Define index route
75
+ @app.route('/', methods=['GET'])
76
+ def index():
77
+ return render_template('index.html')
78
+
79
+ if __name__ == "__main__":
80
+ app.run(debug=True)