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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
import zipfile
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Load the YOLOv5 model (ensure the path is correct)
|
| 9 |
+
model = torch.hub.load(
|
| 10 |
+
'ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp/weights/best.pt'
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Global variables to store images and labels for carousel functionality
|
| 14 |
+
processed_images = []
|
| 15 |
+
label_contents = []
|
| 16 |
+
processed_image_paths = []
|
| 17 |
+
|
| 18 |
+
# Temporary folder for saving processed files
|
| 19 |
+
TEMP_DIR = "temp_processed"
|
| 20 |
+
|
| 21 |
+
# Ensure the temp directory exists
|
| 22 |
+
if not os.path.exists(TEMP_DIR):
|
| 23 |
+
os.makedirs(TEMP_DIR)
|
| 24 |
+
|
| 25 |
+
# Function to process all uploaded images
|
| 26 |
+
def process_images(files):
|
| 27 |
+
global processed_images, label_contents, processed_image_paths
|
| 28 |
+
processed_images = []
|
| 29 |
+
label_contents = []
|
| 30 |
+
processed_image_paths = []
|
| 31 |
+
|
| 32 |
+
# Clear the temp directory
|
| 33 |
+
for f in os.listdir(TEMP_DIR):
|
| 34 |
+
os.remove(os.path.join(TEMP_DIR, f))
|
| 35 |
+
|
| 36 |
+
for i, file_path in enumerate(files):
|
| 37 |
+
# Open the image using the file path
|
| 38 |
+
img = Image.open(file_path)
|
| 39 |
+
|
| 40 |
+
# Run inference
|
| 41 |
+
results = model(img)
|
| 42 |
+
|
| 43 |
+
# Convert results to Image format for display
|
| 44 |
+
results.render()
|
| 45 |
+
if hasattr(results, 'ims'):
|
| 46 |
+
output_image = Image.fromarray(results.ims[0])
|
| 47 |
+
else:
|
| 48 |
+
output_image = Image.fromarray(results.imgs[0])
|
| 49 |
+
|
| 50 |
+
# Save the processed image and its labels
|
| 51 |
+
processed_images.append(output_image)
|
| 52 |
+
|
| 53 |
+
# Generate YOLO-format labels as a string
|
| 54 |
+
label_content = ""
|
| 55 |
+
for *box, conf, cls in results.xywh[0]:
|
| 56 |
+
class_id = int(cls)
|
| 57 |
+
x_center, y_center, width, height = box
|
| 58 |
+
label_content += f"{class_id} {x_center} {y_center} {width} {height}\n"
|
| 59 |
+
label_contents.append(label_content)
|
| 60 |
+
|
| 61 |
+
# Save the image to the temp folder
|
| 62 |
+
image_path = os.path.join(TEMP_DIR, f"processed_image_{i}.png")
|
| 63 |
+
output_image.save(image_path)
|
| 64 |
+
processed_image_paths.append(image_path)
|
| 65 |
+
|
| 66 |
+
# Save the label content to a text file
|
| 67 |
+
label_filename = f"annotation_{i}.txt"
|
| 68 |
+
label_path = os.path.join(TEMP_DIR, label_filename)
|
| 69 |
+
with open(label_path, "w") as label_file:
|
| 70 |
+
label_file.write(label_content)
|
| 71 |
+
|
| 72 |
+
# Return the first image and its labels
|
| 73 |
+
if processed_images:
|
| 74 |
+
return processed_images[0], label_contents[0], 0 # Start with index 0
|
| 75 |
+
else:
|
| 76 |
+
return None, "No images found.", 0
|
| 77 |
+
|
| 78 |
+
# Function to create and return a ZIP file for download
|
| 79 |
+
def create_zip():
|
| 80 |
+
zip_buffer = io.BytesIO()
|
| 81 |
+
with zipfile.ZipFile(zip_buffer, 'w') as z:
|
| 82 |
+
# Add images and labels to the ZIP file
|
| 83 |
+
for image_path in processed_image_paths:
|
| 84 |
+
z.write(image_path, os.path.basename(image_path))
|
| 85 |
+
# Get index from image filename
|
| 86 |
+
image_filename = os.path.basename(image_path)
|
| 87 |
+
base_name, ext = os.path.splitext(image_filename)
|
| 88 |
+
index = base_name.split('_')[-1]
|
| 89 |
+
# Construct label filename
|
| 90 |
+
label_filename = f"annotation_{index}.txt"
|
| 91 |
+
label_path = os.path.join(TEMP_DIR, label_filename)
|
| 92 |
+
z.write(label_path, label_filename)
|
| 93 |
+
|
| 94 |
+
zip_buffer.seek(0) # Go to the start of the buffer
|
| 95 |
+
# Return the bytes of the ZIP file
|
| 96 |
+
return zip_buffer.getvalue()
|
| 97 |
+
|
| 98 |
+
# Function to navigate through images
|
| 99 |
+
def next_image(index):
|
| 100 |
+
global processed_images, label_contents
|
| 101 |
+
if processed_images:
|
| 102 |
+
index = (index + 1) % len(processed_images)
|
| 103 |
+
return processed_images[index], label_contents[index], index
|
| 104 |
+
else:
|
| 105 |
+
return None, "No images processed.", index
|
| 106 |
+
|
| 107 |
+
def prev_image(index):
|
| 108 |
+
global processed_images, label_contents
|
| 109 |
+
if processed_images:
|
| 110 |
+
index = (index - 1) % len(processed_images)
|
| 111 |
+
return processed_images[index], label_contents[index], index
|
| 112 |
+
else:
|
| 113 |
+
return None, "No images processed.", index
|
| 114 |
+
|
| 115 |
+
# Gradio interface
|
| 116 |
+
with gr.Blocks() as interface:
|
| 117 |
+
# Multiple file input and display area
|
| 118 |
+
file_input = gr.Files(label="Upload multiple image files", type="filepath")
|
| 119 |
+
image_display = gr.Image(label="Processed Image")
|
| 120 |
+
label_display = gr.Textbox(label="Label File Content")
|
| 121 |
+
|
| 122 |
+
# Buttons for carousel navigation
|
| 123 |
+
prev_button = gr.Button("Previous Image")
|
| 124 |
+
next_button = gr.Button("Next Image")
|
| 125 |
+
|
| 126 |
+
# Hidden state to store current index
|
| 127 |
+
current_index = gr.State(0)
|
| 128 |
+
|
| 129 |
+
# Button to prepare the ZIP file for download
|
| 130 |
+
prepare_download_button = gr.Button("Prepare Download")
|
| 131 |
+
|
| 132 |
+
# Download button
|
| 133 |
+
download_button = gr.DownloadButton(label="Download ZIP", visible=False)
|
| 134 |
+
|
| 135 |
+
# Define functionality when files are uploaded
|
| 136 |
+
file_input.change(
|
| 137 |
+
process_images,
|
| 138 |
+
inputs=file_input,
|
| 139 |
+
outputs=[image_display, label_display, current_index]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Define functionality for next and previous buttons
|
| 143 |
+
next_button.click(
|
| 144 |
+
next_image,
|
| 145 |
+
inputs=current_index,
|
| 146 |
+
outputs=[image_display, label_display, current_index]
|
| 147 |
+
)
|
| 148 |
+
prev_button.click(
|
| 149 |
+
prev_image,
|
| 150 |
+
inputs=current_index,
|
| 151 |
+
outputs=[image_display, label_display, current_index]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Define functionality for the prepare download button
|
| 155 |
+
def prepare_download():
|
| 156 |
+
data = create_zip()
|
| 157 |
+
return gr.update(value={"name": "processed_images_annotations.zip", "data": data}, visible=True)
|
| 158 |
+
|
| 159 |
+
prepare_download_button.click(
|
| 160 |
+
prepare_download,
|
| 161 |
+
outputs=download_button
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Launch the interface
|
| 165 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|
| 3 |
+
Pillow
|