Spaces:
Build error
Build error
Update app.py
Browse files2 unmodified lines345678910111213141516173 unmodified lines2122232425262728293048 unmodified lines798081828384858687886 unmodified lines959697989910010110210326 unmodified lines13013113213313413513613713813914014 unmodified lines1551561571581591601611621631641651661671681692 unmodified linesfrom PIL import Imageimport osimport zipfileimport ioimport shutil# Clear the torch hub cache to prevent any previous corrupted downloadsshutil.rmtree(torch.hub._get_torch_home(), ignore_errors=True)# Load the YOLOv5 model from the uploaded best.pt filemodel = torch.hub.load('ultralytics/yolov5', 'custom', path='best-3.pt', trust_repo=True)# Global variables to store images and labels for carousel functionality3 unmodified lines# Temporary folder for saving processed filesTEMP_DIR = "temp_processed"# Ensure the temp directory existsif not os.path.exists(TEMP_DIR):os.makedirs(TEMP_DIR)# Function to process all uploaded imagesdef process_images(files):48 unmodified lineselse:return None, "No images found.", 0# Function to create and return a ZIP file for downloaddef create_zip():zip_buffer = io.BytesIO()with zipfile.ZipFile(zip_buffer, 'w') as z:# Add images and labels to the ZIP filefor image_path in processed_image_paths:z.write(image_path, os.path.basename(image_path))6 unmodified lineslabel_path = os.path.join(TEMP_DIR, label_filename)z.write(label_path, label_filename)zip_buffer.seek(0) # Go to the start of the buffer# Return the bytes of the ZIP filereturn zip_buffer.getvalue()# Function to navigate through imagesdef next_image(index):26 unmodified lines# Hidden state to store current indexcurrent_index = gr.State(0)# Button to prepare the ZIP file for downloadprepare_download_button = gr.Button("Prepare Download")# Download buttondownload_button = gr.DownloadButton(label="Download ZIP", visible=False)# Define functionality when files are uploadedfile_input.change(14 unmodified linesoutputs=[image_display, label_display, current_index])# Define functionality for the prepare download buttondef prepare_download():data = create_zip()return gr.update(value={"name": "processed_images_annotations.zip", "data": data}, visible=True)prepare_download_button.click(prepare_download,outputs=download_button)# Launch the interfaceinterface.launch()2 unmodified lines34567891011123 unmodified lines1617181920212248 unmodified lines717273747576777879808182838485866 unmodified lines9394959697989926 unmodified lines12612712812913013113213313414 unmodified lines1491501511521531541551561571581591601611621632 unmodified linesfrom PIL import Imageimport osimport zipfile# Load the YOLOv5 model (ensure the path is correct)model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')# Global variables to store images and labels for carousel functionality3 unmodified lines# Temporary folder for saving processed filesTEMP_DIR = "temp_processed"os.makedirs(TEMP_DIR, exist_ok=True)# Function to process all uploaded imagesdef process_images(files):48 unmodified lineselse:return None, "No images found.", 0# Function to create and return the path to the ZIP file for downloaddef create_zip():zip_filename = "processed_images_annotations.zip"zip_path = os.path.join(TEMP_DIR, zip_filename)# Remove existing ZIP file if it existsif os.path.exists(zip_path):os.remove(zip_path)with zipfile.ZipFile(zip_path, 'w') as z:# Add images and labels to the ZIP filefor image_path in processed_image_paths:z.write(image_path, os.path.basename(image_path))6 unmodified lineslabel_path = os.path.join(TEMP_DIR, label_filename)z.write(label_path, label_filename)return zip_path # Return the file path as a string# Function to navigate through imagesdef next_image(index):26 unmodified lines# Hidden state to store current indexcurrent_index = gr.State(0)# Button to download all processed images and annotations as a ZIP filedownload_button = gr.Button("Prepare and Download All")download_file = gr.File()# Define functionality when files are uploadedfile_input.change(14 unmodified linesoutputs=[image_display, label_display, current_index])# Define functionality for the download button to zip the files and allow downloaddef prepare_download():zip_path = create_zip()return zip_pathdownload_button.click(prepare_download,outputs=download_file)# Launch the interfaceinterface.launch(share=True)
app.py+22-28