panda1835 commited on
Commit
5d1f4e2
·
1 Parent(s): 8456697

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -65
app.py CHANGED
@@ -19,81 +19,28 @@ def detect(image):
19
  left = int(results['xmin'])
20
  width = int(results['xmax'] - results['xmin'])
21
  height = int(results['ymax'] - results['ymin'])
22
- return top, left, width, height
23
- except:
24
- return 0,0,0,0
25
-
26
- def format_label(label):
27
- """
28
- From '0 class 1\n' to 'class 1'
29
- """
30
- return label[:-1]
31
-
32
-
33
- def predict(image):
34
- top, left, width, height = detect(image)
35
-
36
- if (top == 0) and (left == 0) and (width == 0) and (height==0):
37
  return {
38
- "predictions": {},
39
  'bbox': {
40
- "top": 0,
41
- "left": 0,
42
- "width": 0,
43
- "height": 0
44
  }
45
  }
46
-
47
- if width > height:
48
- height = width
49
- else:
50
- width = height
51
-
52
- # Crop the turtle
53
- image = image.crop((left, top, left + width, top + height))
54
-
55
- # Create the array of the right shape to feed into the keras model
56
- # The 'length' or number of images you can put into the array is
57
- # determined by the first position in the shape tuple, in this case 1.
58
- data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
59
-
60
- #resize the image to a 224x224 with the same strategy as in TM2:
61
- #resizing the image to be at least 224x224 and then cropping from the center
62
- size = (224, 224)
63
- image = ImageOps.fit(image, size, Image.ANTIALIAS)
64
-
65
- #turn the image into a numpy array
66
- image_array = np.asarray(image)
67
- # Normalize the image
68
- normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
69
- # Load the image into the array
70
- data[0] = normalized_image_array
71
-
72
- # run the inference
73
- pred = classify_model.predict(data)
74
- pred = pred.tolist()
75
-
76
- with open('labels.txt','r') as f:
77
- labels = f.readlines()
78
-
79
- result = {format_label(labels[i]): round(pred[0][i],2) for i in range(len(pred[0]))}
80
- sorted_result = {k: v for k, v in sorted(result.items(), key=lambda item: item[1], reverse=True) if v > 0}
81
-
82
-
83
- return json.dumps({
84
- "predictions": sorted_result,
85
  'bbox': {
86
- "top": top,
87
- "left": left,
88
- "width": width,
89
- "height": height
90
  }
91
- })
92
 
93
  title = "🐆"
94
 
95
  gr.Interface(
96
- fn=predict,
97
  inputs=gr.Image(type="pil", label="Input Image"),
98
  outputs=[gr.JSON()],
99
  # live=True,
 
19
  left = int(results['xmin'])
20
  width = int(results['xmax'] - results['xmin'])
21
  height = int(results['ymax'] - results['ymin'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  return {
 
23
  'bbox': {
24
+ "top": top,
25
+ "left": left,
26
+ "width": width,
27
+ "height": height
28
  }
29
  }
30
+ except:
31
+ return {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  'bbox': {
33
+ "top": 0,
34
+ "left": 0,
35
+ "width": 0,
36
+ "height": 0
37
  }
38
+ }
39
 
40
  title = "🐆"
41
 
42
  gr.Interface(
43
+ fn=detect,
44
  inputs=gr.Image(type="pil", label="Input Image"),
45
  outputs=[gr.JSON()],
46
  # live=True,