Devon12 commited on
Commit
31b3b50
·
verified ·
1 Parent(s): 505529e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -24
app.py CHANGED
@@ -11,54 +11,68 @@ def yolov8_func(image,
11
 
12
  # Load the YOLOv8 model
13
  model_path = "best.pt"
14
- model = YOLO(model_path)
15
-
16
  # Make predictions
17
  result = model.predict(image, conf=conf_thresold, iou=iou_thresold, imgsz=image_size)
18
 
19
  # Access object detection results
20
- boxes = result[0].boxes
21
- num_boxes = len(boxes)
 
 
 
 
 
 
22
 
23
  # Categorize based on number of boxes (detections) and provide recommendations
24
- if num_boxes > 10:
25
  severity = "Worse"
26
  recommendation = "It is recommended to see a dermatologist and start stronger acne treatment."
27
- elif 5 <= num_boxes <= 10:
28
  severity = "Medium"
29
  recommendation = "You should follow a consistent skincare routine with proper cleansing and moisturizing."
30
  else:
31
  severity = "Good"
32
  recommendation = "Your skin looks good! Keep up with your current skincare routine."
33
 
 
 
 
34
  # Render the result (with bounding boxes/labels)
35
  render = render_result(model=model, image=image, result=result[0])
36
 
 
37
  predicted_image_save_path = "predicted_image.jpg"
38
  render.save(predicted_image_save_path)
 
 
39
  return predicted_image_save_path, f"Acne condition: {severity}", recommendation
40
 
41
- # Create the Gradio
42
- with gr.Blocks() as yolo_app:
43
- gr.Markdown("# YOLOv8: An Object Detection for Acne")
 
 
 
 
44
 
45
- with gr.Row():
46
- with gr.Column(scale=1): # Left side with input
47
- input_image = gr.Image(type="filepath", label="Input Image")
48
- image_size = gr.Slider(minimum=320, maximum=1280, step=32, value=640, label="Image Size")
49
- conf_thresh = gr.Slider(minimum=0, maximum=1, step=0.05, value=0.15, label="Confidence Threshold")
50
- iou_thresh = gr.Slider(minimum=0, maximum=1, step=0.05, value=0.2, label="IOU Threshold")
51
- submit_btn = gr.Button("Submit")
52
 
53
- with gr.Column(scale=1): # Right side with output
54
- output_image = gr.Image(type="filepath", label="Output Image")
55
- acne_condition = gr.Textbox(label="Acne Condition")
56
- recommendation = gr.Textbox(label="Recommendation")
57
 
58
- # Link the submit button to the function
59
- submit_btn.click(fn=yolov8_func,
60
- inputs=[input_image, image_size, conf_thresh, iou_thresh],
61
- outputs=[output_image, acne_condition, recommendation])
 
62
 
63
  # Launch the app
64
  yolo_app.launch(debug=True)
 
11
 
12
  # Load the YOLOv8 model
13
  model_path = "best.pt"
14
+ model = YOLO(model_path) # Use your custom model path here
15
+
16
  # Make predictions
17
  result = model.predict(image, conf=conf_thresold, iou=iou_thresold, imgsz=image_size)
18
 
19
  # Access object detection results
20
+ boxes = result[0].boxes # Bounding boxes
21
+ num_boxes = len(boxes) # Count the number of bounding boxes (detections)
22
+
23
+ # Print object detection details (optional)
24
+ print("Object type: ", boxes.cls)
25
+ print("Confidence: ", boxes.conf)
26
+ print("Coordinates: ", boxes.xyxy)
27
+ print(f"Number of bounding boxes: {num_boxes}")
28
 
29
  # Categorize based on number of boxes (detections) and provide recommendations
30
+ if num_boxes > 20:
31
  severity = "Worse"
32
  recommendation = "It is recommended to see a dermatologist and start stronger acne treatment."
33
+ elif 10 <= num_boxes <= 20:
34
  severity = "Medium"
35
  recommendation = "You should follow a consistent skincare routine with proper cleansing and moisturizing."
36
  else:
37
  severity = "Good"
38
  recommendation = "Your skin looks good! Keep up with your current skincare routine."
39
 
40
+ print(f"Acne condition: {severity}")
41
+ print(f"Recommendation: {recommendation}")
42
+
43
  # Render the result (with bounding boxes/labels)
44
  render = render_result(model=model, image=image, result=result[0])
45
 
46
+ # Save the rendered image (with predictions)
47
  predicted_image_save_path = "predicted_image.jpg"
48
  render.save(predicted_image_save_path)
49
+
50
+ # Return the saved image, severity, and recommendation for Gradio output
51
  return predicted_image_save_path, f"Acne condition: {severity}", recommendation
52
 
53
+ # Define inputs for the Gradio app
54
+ inputs = [
55
+ gr.Image(type="filepath", label="Input Image"),
56
+ gr.Slider(minimum=320, maximum=1280, step=32, value=640, label="Image Size"),
57
+ gr.Slider(minimum=0, maximum=1, step=0.05, value=0.15, label="Confidence Threshold"),
58
+ gr.Slider(minimum=0, maximum=1, step=0.05, value=0.2, label="IOU Threshold")
59
+ ]
60
 
61
+ # Define the output for the Gradio app (image + text for severity and recommendation)
62
+ outputs = [
63
+ gr.Image(type="filepath", label="Output Image"),
64
+ gr.Textbox(label="Acne Condition"),
65
+ gr.Textbox(label="Recommendation")
66
+ ]
 
67
 
68
+ # Set the title of the Gradio app
69
+ title = "YOLOv8: An Object Detection for Acne"
 
 
70
 
71
+ # Create the Gradio interface
72
+ yolo_app = gr.Interface(fn=yolov8_func,
73
+ inputs=inputs,
74
+ outputs=outputs,
75
+ title=title)
76
 
77
  # Launch the app
78
  yolo_app.launch(debug=True)