MUmerBhutta67 commited on
Commit
1d8ebc1
·
verified ·
1 Parent(s): e898f11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -41
app.py CHANGED
@@ -4,65 +4,61 @@ from sahi.prediction import ObjectPrediction
4
  from sahi.utils.cv import visualize_object_predictions, read_image
5
  from ultralyticsplus import YOLO, render_result
6
 
 
7
  image_path = [
8
  ['test/web form.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45],
9
  ['test/web form2.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45]
10
  ]
11
 
12
-
13
  def yolov8_inference(
14
- image: gr.inputs.Image = None,
15
- model_path: gr.inputs.Dropdown = None,
16
- image_size: gr.inputs.Slider = 640,
17
- conf_threshold: gr.inputs.Slider = 0.25,
18
- iou_threshold: gr.inputs.Slider = 0.45,
19
  ):
20
- """
21
- YOLOv8 inference function
22
- Args:
23
- image: Input image
24
- model_path: Path to the model
25
- image_size: Image size
26
- conf_threshold: Confidence threshold
27
- iou_threshold: IOU threshold
28
- Returns:
29
- Rendered image
30
- """
31
  model = YOLO(model_path)
32
- model.overrides['conf'] = conf_threshold
33
- model.overrides['iou']= iou_threshold
34
- model.overrides['agnostic_nms'] = False # NMS class-agnostic
35
- model.overrides['max_det'] = 1000
36
- image = read_image(image)
37
- results = model.predict(image)
38
- render = render_result(model=model, image=image, result=results[0])
39
-
40
- return render
41
-
 
 
42
 
 
43
  inputs = [
44
- gr.inputs.Image(type="filepath", label="Input Image"),
45
- gr.inputs.Dropdown(["foduucom/web-form-ui-field-detection"],
46
- default="foduucom/web-form-ui-field-detection", label="Model"),
47
- gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
48
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
49
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
 
 
 
50
  ]
51
 
52
- outputs = gr.outputs.Image(type="filepath", label="Output Image")
53
- title = "Ui form : web form ui field Detection in Images"
 
54
 
55
- interface_image = gr.Interface(
 
56
  fn=yolov8_inference,
57
  inputs=inputs,
58
  outputs=outputs,
59
  title=title,
60
  examples=image_path,
61
  cache_examples=False,
62
- theme='huggingface'
63
  )
64
 
65
- gr.TabbedInterface(
66
- [interface_image],
67
- tab_names=['Image inference']
68
- ).queue().launch()
 
4
  from sahi.utils.cv import visualize_object_predictions, read_image
5
  from ultralyticsplus import YOLO, render_result
6
 
7
+ # your example images
8
  image_path = [
9
  ['test/web form.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45],
10
  ['test/web form2.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45]
11
  ]
12
 
 
13
  def yolov8_inference(
14
+ image, # will be a filepath string
15
+ model_path, # string
16
+ image_size, # int
17
+ conf_threshold, # float
18
+ iou_threshold # float
19
  ):
20
+ # load and configure the model
 
 
 
 
 
 
 
 
 
 
21
  model = YOLO(model_path)
22
+ model.overrides.update({
23
+ 'conf': conf_threshold,
24
+ 'iou': iou_threshold,
25
+ 'agnostic_nms': False,
26
+ 'max_det': 1000
27
+ })
28
+
29
+ # read & run
30
+ img = read_image(image)
31
+ results = model.predict(img)
32
+ rendered = render_result(model=model, image=img, result=results[0])
33
+ return rendered
34
 
35
+ # define components using the new API
36
  inputs = [
37
+ gr.Image(type="filepath", label="Input Image"),
38
+ gr.Dropdown(
39
+ choices=["foduucom/web-form-ui-field-detection"],
40
+ value="foduucom/web-form-ui-field-detection",
41
+ label="Model"
42
+ ),
43
+ gr.Slider(320, 1280, step=32, value=640, label="Image Size"),
44
+ gr.Slider(0.0, 1.0, step=0.05, value=0.25, label="Confidence Threshold"),
45
+ gr.Slider(0.0, 1.0, step=0.05, value=0.45, label="IOU Threshold"),
46
  ]
47
 
48
+ outputs = gr.Image(type="filepath", label="Output Image")
49
+
50
+ title = "Web-Form UI Field Detection"
51
 
52
+ # single-tab interface
53
+ interface = gr.Interface(
54
  fn=yolov8_inference,
55
  inputs=inputs,
56
  outputs=outputs,
57
  title=title,
58
  examples=image_path,
59
  cache_examples=False,
60
+ theme="huggingface"
61
  )
62
 
63
+ if __name__ == "__main__":
64
+ interface.launch()