atlury commited on
Commit
682c5ed
·
verified ·
1 Parent(s): 4dee5e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -5,6 +5,14 @@ import numpy as np
5
  import os
6
  import requests
7
  import torch
 
 
 
 
 
 
 
 
8
 
9
  # Load the model file
10
  model_path = "yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
@@ -16,41 +24,35 @@ if not os.path.exists(model_path):
16
  f.write(response.content)
17
 
18
  # Load the document segmentation model
19
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
20
- docseg_model = YOLO(model_path) # Remove .to(device) to let ultralytics auto-detect
 
 
 
21
 
22
  def process_image(image):
23
  try:
24
  # Convert image to the format YOLO model expects
25
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
26
- results = docseg_model.predict(image) # Use predict for inference
 
 
 
 
 
27
  result = results[0] # Get the first (and usually only) result
28
 
29
  # Extract annotated image from results
30
- annotated_img = result.plot() # Simplified plotting
31
  annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
32
 
33
  # Prepare detected areas and labels as text output
34
  detected_areas_labels = "\n".join(
35
- [f"{box.label.upper()}: {box.conf:.2f}" for box in result.boxes] # Uppercase labels
36
  )
37
  except Exception as e:
38
  return None, f"Error during processing: {e}" # Error handling
39
 
40
  return annotated_img, detected_areas_labels
41
 
42
- # Define the Gradio interface
43
- with gr.Blocks() as interface:
44
- gr.Markdown("### Document Segmentation using YOLOv8")
45
- input_image = gr.Image(type="pil", label="Input Image")
46
- output_image = gr.Image(type="pil", label="Annotated Image")
47
- output_text = gr.Textbox(label="Detected Areas and Labels")
48
-
49
- gr.Button("Run").click(
50
- fn=process_image,
51
- inputs=input_image,
52
- outputs=[output_image, output_text]
53
- )
54
-
55
- # Launch the interface (remove the conditional launch)
56
- interface.launch(share=True) # Allow sharing for easier debugging
 
5
  import os
6
  import requests
7
  import torch
8
+ import huggingface_hub
9
+
10
+ # Initialize ZeroGPU
11
+ zero_gpu_is_available = huggingface_hub.utils.is_google_colab() or huggingface_hub.utils.is_notebook()
12
+ if zero_gpu_is_available:
13
+ from accelerate import Accelerator
14
+ accelerator = Accelerator()
15
+
16
 
17
  # Load the model file
18
  model_path = "yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
 
24
  f.write(response.content)
25
 
26
  # Load the document segmentation model
27
+ docseg_model = YOLO(model_path)
28
+
29
+ if zero_gpu_is_available:
30
+ docseg_model.to(accelerator.device) # Put the model on the accelerator's device.
31
+
32
 
33
  def process_image(image):
34
  try:
35
  # Convert image to the format YOLO model expects
36
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
37
+
38
+ # If Zero GPU, move image to accelerator
39
+ if zero_gpu_is_available:
40
+ image = torch.from_numpy(image).to(accelerator.device)
41
+
42
+ results = docseg_model.predict(image)
43
  result = results[0] # Get the first (and usually only) result
44
 
45
  # Extract annotated image from results
46
+ annotated_img = result.plot()
47
  annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
48
 
49
  # Prepare detected areas and labels as text output
50
  detected_areas_labels = "\n".join(
51
+ [f"{box.label.upper()}: {box.conf:.2f}" for box in result.boxes]
52
  )
53
  except Exception as e:
54
  return None, f"Error during processing: {e}" # Error handling
55
 
56
  return annotated_img, detected_areas_labels
57
 
58
+ # The rest of the code remains the same (Gradio interface)