skngew commited on
Commit
71626de
·
verified ·
1 Parent(s): f18a10a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -37
app.py CHANGED
@@ -1,55 +1,60 @@
1
- from ultralytics import YOLO
2
- from PIL import Image
3
  import gradio as gr
 
4
  from huggingface_hub import snapshot_download
5
  import os
6
 
7
 
 
8
  def load_model(repo_id):
9
  download_dir = snapshot_download(repo_id)
10
- print(download_dir)
11
- path = os.path.join(download_dir, "best_int8_openvino_model")
12
- print(path)
13
- detection_model = YOLO(path, task='detect')
14
- return detection_model
15
 
16
 
17
- def predict(pilimg, confidence, iou):
18
- source = pilimg
19
- result = detection_model.predict(source, conf=confidence, iou=iou)
20
- img_bgr = result[0].plot()
21
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
22
- return out_pilimg
 
23
 
24
 
25
- REPO_ID = "skngew/9053220B"
26
- detection_model = load_model(REPO_ID)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Student ID
29
  student_id = "Student ID: 9053220B"
30
 
31
  # Create the Gradio interface
32
- def create_interface():
33
- # Persistent state for default values
34
- confidence_default = gr.State(0.5)
35
- iou_default = gr.State(0.6)
36
-
37
- interface = gr.Interface(
38
- fn=predict,
39
- inputs=[
40
- gr.Image(type="pil", label="Input Image"),
41
- gr.Slider(0, 1, value=confidence_default.value, label="Confidence Threshold"), # Default to 0.5
42
- gr.Slider(0, 1, value=iou_default.value, label="IOU Threshold") # Default to 0.6
43
- ],
44
- outputs=gr.Image(type="pil", label="Output Image"),
45
- title="Object Detection with YOLOv8",
46
- description=student_id,
47
- live=False,
48
- )
49
-
50
- return interface
51
-
52
 
53
  # Launch the Gradio app
54
- app_interface = create_interface()
55
- app_interface.launch(share=True)
 
1
+ import numpy as np
2
+ import tensorflow as tf
3
  import gradio as gr
4
+ from tensorflow.keras.preprocessing import image
5
  from huggingface_hub import snapshot_download
6
  import os
7
 
8
 
9
+ # Load the model from Hugging Face Hub
10
  def load_model(repo_id):
11
  download_dir = snapshot_download(repo_id)
12
+ model_path = os.path.join(download_dir, "full_model.weights.h5")
13
+ model = tf.keras.models.load_model(model_path)
14
+ return model
 
 
15
 
16
 
17
+ # Function to preprocess the uploaded image
18
+ def preprocess_image(img, target_size=(224, 224)):
19
+ img = img.resize(target_size) # Resize to match model input
20
+ img_array = image.img_to_array(img)
21
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
22
+ img_array = tf.keras.applications.efficientnet.preprocess_input(img_array)
23
+ return img_array
24
 
25
 
26
+ # Perform inference
27
+ def predict(image_input):
28
+ class_names = ["Defective Tyre", "Good Tyre"]
29
+
30
+ # Preprocess image
31
+ img_array = preprocess_image(image_input)
32
+
33
+ # Get prediction
34
+ prediction = model.predict(img_array)[0][0] # Scalar sigmoid output
35
+ predicted_class_idx = int(prediction >= 0.5) # 0 if <0.5, 1 if >=0.5
36
+ predicted_class = class_names[predicted_class_idx] # Get class name
37
+
38
+ return f"Predicted Class: {predicted_class} (Confidence: {prediction:.5f})"
39
+
40
+
41
+ # Hugging Face Model Repository ID
42
+ REPO_ID = "skngew/9053220B" # Change this to your actual repo ID
43
+
44
+ # Load the model
45
+ model = load_model(REPO_ID)
46
 
47
  # Student ID
48
  student_id = "Student ID: 9053220B"
49
 
50
  # Create the Gradio interface
51
+ interface = gr.Interface(
52
+ fn=predict,
53
+ inputs=gr.Image(type="pil", label="Upload an Image"),
54
+ outputs=gr.Textbox(label="Prediction"),
55
+ title="Binary Classification: Good vs. Defective Tire",
56
+ description=student_id,
57
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Launch the Gradio app
60
+ interface.launch(share=True)