sabaridsnfuji commited on
Commit
86c8f1b
·
verified ·
1 Parent(s): 1aa0de7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -11,6 +11,7 @@ import os
11
  import json
12
  from openvino.runtime import Core # Assuming you're using OpenVINO
13
  from tf_post_processing import non_max_suppression # Assuming this is defined elsewhere
 
14
 
15
  # Load the OpenVINO model
16
  classification_model_xml = "./model/best.xml"
@@ -39,6 +40,10 @@ label_to_class_text = {
39
 
40
  # Function to perform inference
41
  def predict_image(image):
 
 
 
 
42
  # Resize, preprocess, and reshape the input image
43
  img_size = 960
44
  resized_image = cv2.resize(image, (img_size, img_size)) / 255.0
@@ -89,20 +94,14 @@ def predict_image(image):
89
  }
90
  })
91
 
92
- # Save the processed image and JSON file
93
  output_image_path = os.path.join(output_image_folder, "result_image.jpg")
94
  cv2.imwrite(output_image_path, image)
95
 
96
- output_json_path = os.path.join(output_json_folder, "predictions.json")
97
- with open(output_json_path, 'w') as f:
98
- json.dump(predictions, f, indent=4)
99
-
100
- return image, predictions
101
 
102
- # Set up Gradio interface with sample images and image upload
103
- def gradio_interface(image):
104
- output_image, predictions = predict_image(image)
105
- return output_image, json.dumps(predictions, indent=4)
106
 
107
  # Define sample images for user convenience
108
  sample_images = [
@@ -111,11 +110,16 @@ sample_images = [
111
  "./sample/10_12.jpg"
112
  ]
113
 
 
 
 
 
 
114
  # Create the Gradio Interface
115
  gr_interface = gr.Interface(
116
  fn=gradio_interface,
117
- inputs=gr.inputs.Image(label="Upload or Select an Image", type="numpy", sample_images=sample_images),
118
- outputs=[gr.outputs.Image(label="Result Image"), gr.outputs.Textbox(label="Predictions JSON")],
119
  title="OpenVINO Model Inference with Gradio",
120
  description="Upload an image or select a sample image to get model predictions with bounding boxes and probabilities."
121
  )
 
11
  import json
12
  from openvino.runtime import Core # Assuming you're using OpenVINO
13
  from tf_post_processing import non_max_suppression # Assuming this is defined elsewhere
14
+ from PIL import Image
15
 
16
  # Load the OpenVINO model
17
  classification_model_xml = "./model/best.xml"
 
40
 
41
  # Function to perform inference
42
  def predict_image(image):
43
+ # Convert the Pillow image to a NumPy array and BGR format for OpenCV
44
+ image = np.array(image.convert("RGB"))
45
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
46
+
47
  # Resize, preprocess, and reshape the input image
48
  img_size = 960
49
  resized_image = cv2.resize(image, (img_size, img_size)) / 255.0
 
94
  }
95
  })
96
 
97
+ # Save the processed image
98
  output_image_path = os.path.join(output_image_folder, "result_image.jpg")
99
  cv2.imwrite(output_image_path, image)
100
 
101
+ # Convert predictions to a formatted string
102
+ predictions_str = json.dumps(predictions, indent=4)
 
 
 
103
 
104
+ return image, predictions_str
 
 
 
105
 
106
  # Define sample images for user convenience
107
  sample_images = [
 
110
  "./sample/10_12.jpg"
111
  ]
112
 
113
+ # Set up Gradio interface
114
+ def gradio_interface(image):
115
+ output_image, predictions_str = predict_image(image)
116
+ return output_image, predictions_str
117
+
118
  # Create the Gradio Interface
119
  gr_interface = gr.Interface(
120
  fn=gradio_interface,
121
+ inputs=gr.Image(label="Upload or Select an Image", type="pil", sample_images=sample_images),
122
+ outputs=[gr.Image(label="Result Image"), gr.Textbox(label="Predictions JSON")],
123
  title="OpenVINO Model Inference with Gradio",
124
  description="Upload an image or select a sample image to get model predictions with bounding boxes and probabilities."
125
  )