lkp72 commited on
Commit
0a20256
·
verified ·
1 Parent(s): 3683a03

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openvino.inference_engine import IECore
3
+ import cv2
4
+ import numpy as np
5
+
6
+ # Load the OpenVINO model
7
+ ie = IECore()
8
+ net = ie.read_network(model='openvino/best.xml', weights='openvino/best.bin')
9
+ exec_net = ie.load_network(network=net, device_name="CPU")
10
+
11
+ # Define a function for prediction
12
+ def predict(image):
13
+ # Preprocess the image
14
+ input_blob = next(iter(net.input_info))
15
+ input_shape = net.input_info[input_blob].input_data.shape
16
+ image_resized = cv2.resize(image, (input_shape[3], input_shape[2]))
17
+ image_transposed = image_resized.transpose((2, 0, 1))
18
+ image_batch = np.expand_dims(image_transposed, axis=0)
19
+
20
+ # Perform inference
21
+ result = exec_net.infer(inputs={input_blob: image_batch})
22
+ output_blob = next(iter(net.outputs))
23
+ detections = result[output_blob]
24
+
25
+ # Post-process the results
26
+ boxes = []
27
+ for detection in detections[0][0]:
28
+ confidence = detection[2]
29
+ if confidence > 0.5: # Threshold
30
+ xmin = int(detection[3] * image.shape[1])
31
+ ymin = int(detection[4] * image.shape[0])
32
+ xmax = int(detection[5] * image.shape[1])
33
+ ymax = int(detection[6] * image.shape[0])
34
+ boxes.append((xmin, ymin, xmax, ymax, confidence))
35
+
36
+ # Draw bounding boxes on the image
37
+ for (xmin, ymin, xmax, ymax, confidence) in boxes:
38
+ cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
39
+
40
+ return image
41
+
42
+ # Create a Gradio interface
43
+ iface = gr.Interface(fn=predict, inputs=gr.Image(type="numpy"), outputs=gr.Image(type="numpy"))
44
+
45
+ # Launch the Gradio app
46
+ iface.launch()