ksh-nyp commited on
Commit
506deee
1 Parent(s): 30da8d8

Upload 4 files

Browse files

Custom TF object detection for leopards and deers.

.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ test_samples/gwen-weustink-I3C1sSXj1i8-unsplash.jpg filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import numpy as np
3
+ from six import BytesIO
4
+ from PIL import Image
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.utils import ops as utils_op
9
+ import tarfile
10
+ import wget
11
+ import gradio as gr
12
+ from huggingface_hub import snapshot_download
13
+ import os
14
+
15
+ PATH_TO_LABELS = 'data/label_map.pbtxt'
16
+ category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
17
+
18
+ def pil_image_as_numpy_array(pilimg):
19
+
20
+ img_array = tf.keras.utils.img_to_array(pilimg)
21
+ img_array = np.expand_dims(img_array, axis=0)
22
+ return img_array
23
+
24
+ def load_image_into_numpy_array(path):
25
+
26
+ image = None
27
+ image_data = tf.io.gfile.GFile(path, 'rb').read()
28
+ image = Image.open(BytesIO(image_data))
29
+ return pil_image_as_numpy_array(image)
30
+
31
+ def load_model():
32
+ download_dir = snapshot_download(REPO_ID)
33
+ saved_model_dir = os.path.join(download_dir, "saved_model")
34
+ detection_model = tf.saved_model.load(saved_model_dir)
35
+ return detection_model
36
+
37
+
38
+ def predict(pilimg):
39
+
40
+ image_np = pil_image_as_numpy_array(pilimg)
41
+ return predict2(image_np)
42
+
43
+ def predict2(image_np):
44
+
45
+ results = detection_model(image_np)
46
+
47
+ # different object detection models have additional results
48
+ result = {key:value.numpy() for key,value in results.items()}
49
+
50
+ label_id_offset = 0
51
+ image_np_with_detections = image_np.copy()
52
+
53
+ viz_utils.visualize_boxes_and_labels_on_image_array(
54
+ image_np_with_detections[0],
55
+ result['detection_boxes'][0],
56
+ (result['detection_classes'][0] + label_id_offset).astype(int),
57
+ result['detection_scores'][0],
58
+ category_index,
59
+ use_normalized_coordinates=True,
60
+ max_boxes_to_draw=200,
61
+ min_score_thresh=.60,
62
+ agnostic_mode=False,
63
+ line_thickness=2)
64
+
65
+ result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
66
+
67
+ return result_pil_img
68
+
69
+
70
+ REPO_ID = "ksh-nyp/TFODModel-1-LeoD"
71
+ detection_model = load_model()
72
+ # pil_image = Image.open(image_path)
73
+ # image_arr = pil_image_as_numpy_array(pil_image)
74
+
75
+ # predicted_img = predict(image_arr)
76
+ # predicted_img.save('predicted.jpg')
77
+
78
+ gr.Interface(fn=predict,
79
+ inputs=gr.Image(type="pil"),
80
+ outputs=gr.Image(type="pil")
81
+ ).launch(share=True)
data/label_map.pbtxt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ item {
2
+ id: 1
3
+ name: 'Leopard'
4
+ }
5
+ item {
6
+ id: 2
7
+ name: 'Deer'
8
+ }
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #tf2-tensorflow-object-detection-api
2
+ tf-models-research-object-detection
3
+ matplotlib
4
+ wget
5
+ Pillow==9.5
6
+ huggingface_hub
test_samples/gwen-weustink-I3C1sSXj1i8-unsplash.jpg ADDED

Git LFS Details

  • SHA256: ef1a373e15873c7cf14053775548caad74aacfd3de2818202c60767acabd995e
  • Pointer size: 132 Bytes
  • Size of remote file: 2.29 MB