Spaces:
Sleeping
Sleeping
Update app.py
Browse files5 unmodified lines67891011121 unmodified line14151617181919 unmodified lines39404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596972 unmodified lines1001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481495 unmodified linesimport torchimport numpy as npfrom torchvision.utils import save_image# Import files from the local folderroot_path = os.path.abspath('.')1 unmodified linefrom test_code.inference import super_resolve_imgfrom test_code.test_utils import load_grl, load_rrdb, load_datdef auto_download_if_needed(weight_path):if os.path.exists(weight_path):19 unmodified linesos.system("mv 4x_APISR_DAT_GAN_generator.pth pretrained")def inference(img_path, model_name):try:# Load the modelif model_name == "4xGRL":weight_path = "pretrained/4x_APISR_GRL_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_grl(weight_path, scale=4)generator = generator.to(device='cpu')elif model_name == "4xRRDB":weight_path = "pretrained/4x_APISR_RRDB_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_rrdb(weight_path, scale=4)generator = generator.to(device='cpu')elif model_name == "2xRRDB":weight_path = "pretrained/2x_APISR_RRDB_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_rrdb(weight_path, scale=2)generator = generator.to(device='cpu')elif model_name == "4xDAT":weight_path = "pretrained/4x_APISR_DAT_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_dat(weight_path, scale=4)generator = generator.to(device='cpu')else:raise gr.Error("We don't support such Model")print("We are processing ", img_path)print("The time now is ", datetime.datetime.now(pytz.timezone('US/Eastern')))# In default, we will automatically use crop to match 4x sizesuper_resolved_img = super_resolve_img(generator, img_path, output_path=None, downsample_threshold=720, crop_for_4x=True)store_name = str(time.time()) + ".png"save_image(super_resolved_img, store_name)outputs = cv2.imread(store_name)outputs = cv2.cvtColor(outputs, cv2.COLOR_RGB2BGR)os.remove(store_name)return outputsexcept Exception as error:raise gr.Error(f"global exception: {error}")if __name__ == '__main__':MARKDOWN = \"""## <p style='text-align: center'> APISR: Anime Production Inspired Real-World Anime Super-Resolution (CVPR 2024) </p>2 unmodified linesAPISR aims at restoring and enhancing low-quality low-resolution **anime** images and video sources with various degradations from real-world scenarios.### Note: Due to memory restriction, all images whose short side is over 720 pixel will be downsampled to 720 pixel with the same aspect ratio. E.g., 1920x1080 -> 1280x720### Note: Please check [Model Zoo](https://github.com/Kiteretsu77/APISR/blob/main/docs/model_zoo.md) for the description of each weight and [Here](https://imgsli.com/MjU0MjI0) for model comparisons.### If APISR is helpful, please help star the [GitHub Repo](https://github.com/Kiteretsu77/APISR). Thanks! ###"""block = gr.Blocks().queue(max_size=10)with block:with gr.Row():gr.Markdown(MARKDOWN)with gr.Row(elem_classes=["container"]):with gr.Column(scale=2):input_image = gr.Image(type="filepath", label="Input")model_name = gr.Dropdown(["2xRRDB","4xRRDB","4xGRL","4xDAT",],type="value",value="4xGRL",label="model",)run_btn = gr.Button(value="Submit")with gr.Column(scale=3):output_image = gr.Image(type="numpy", label="Output image")with gr.Row(elem_classes=["container"]):gr.Examples([["__assets__/lr_inputs/image-00277.png"],["__assets__/lr_inputs/image-00542.png"],["__assets__/lr_inputs/41.png"],["__assets__/lr_inputs/f91.jpg"],["__assets__/lr_inputs/image-00440.png"],["__assets__/lr_inputs/image-00164.jpg"],["__assets__/lr_inputs/img_eva.jpeg"],["__assets__/lr_inputs/naruto.jpg"],],[input_image],)run_btn.click(inference, inputs=[input_image, model_name], outputs=[output_image])block.launch()5 unmodified lines6789101112131415161 unmodified line18192021222324252627282930313233343519 unmodified lines5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053062 unmodified lines3093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114125 unmodified linesimport torchimport numpy as npfrom torchvision.utils import save_imageimport jsonimport threadingfrom queue import Queuefrom pathlib import Pathimport shutil# Import files from the local folderroot_path = os.path.abspath('.')1 unmodified linefrom test_code.inference import super_resolve_imgfrom test_code.test_utils import load_grl, load_rrdb, load_dat# Global configurationOUTPUT_DIR = "outputs"HISTORY_FILE = "history.json"VIDEO_QUEUE_FILE = "video_queue.json"video_queue = Queue()processing_status = {}# Initialize directoriesos.makedirs(OUTPUT_DIR, exist_ok=True)os.makedirs(os.path.join(OUTPUT_DIR, "images"), exist_ok=True)os.makedirs(os.path.join(OUTPUT_DIR, "videos"), exist_ok=True)def auto_download_if_needed(weight_path):if os.path.exists(weight_path):19 unmodified linesos.system("mv 4x_APISR_DAT_GAN_generator.pth pretrained")def load_history():"""Load processing history from JSON file"""if os.path.exists(HISTORY_FILE):with open(HISTORY_FILE, 'r') as f:return json.load(f)return []def save_history(history):"""Save processing history to JSON file"""with open(HISTORY_FILE, 'w') as f:json.dump(history, f, indent=2)def add_to_history(input_path, output_path, model_name, process_type, status="completed"):"""Add a record to history"""history = load_history()record = {"timestamp": datetime.datetime.now().isoformat(),"input_path": input_path,"output_path": output_path,"model_name": model_name,"process_type": process_type,"status": status}history.insert(0, record) # Add to beginningsave_history(history)def load_generator(model_name):"""Load the appropriate model"""if model_name == "4xGRL":weight_path = "pretrained/4x_APISR_GRL_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_grl(weight_path, scale=4)elif model_name == "4xRRDB":weight_path = "pretrained/4x_APISR_RRDB_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_rrdb(weight_path, scale=4)elif model_name == "2xRRDB":weight_path = "pretrained/2x_APISR_RRDB_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_rrdb(weight_path, scale=2)elif model_name == "4xDAT":weight_path = "pretrained/4x_APISR_DAT_GAN_generator.pth"auto_download_if_needed(weight_path)generator = load_dat(weight_path, scale=4)else:raise ValueError(f"Model {model_name} not supported")return generator.to(device='cpu')def inference_image(img_path, model_name):"""Process a single image"""try:generator = load_generator(model_name)print("Processing image:", img_path)print("Time:", datetime.datetime.now(pytz.timezone('US/Eastern')))# Process imagesuper_resolved_img = super_resolve_img(generator, img_path, output_path=None,downsample_threshold=720, crop_for_4x=True)# Save outputtimestamp = int(time.time() * 1000)output_name = f"image_{timestamp}.png"output_path = os.path.join(OUTPUT_DIR, "images", output_name)save_image(super_resolved_img, output_path)# Load and convert for displayoutputs = cv2.imread(output_path)outputs = cv2.cvtColor(outputs, cv2.COLOR_BGR2RGB)# Add to historyadd_to_history(img_path, output_path, model_name, "image")return outputs, f"β Saved to: {output_path}"except Exception as error:raise gr.Error(f"Error processing image: {error}")def process_video_frame_by_frame(video_path, model_name, task_id):"""Process video frame by frame"""try:processing_status[task_id] = {"status": "processing", "progress": 0}# Load modelgenerator = load_generator(model_name)# Open videocap = cv2.VideoCapture(video_path)if not cap.isOpened():raise ValueError("Cannot open video file")# Get video propertiesfps = cap.get(cv2.CAP_PROP_FPS)total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# Prepare outputtimestamp = int(time.time() * 1000)output_name = f"video_{timestamp}.mp4"output_path = os.path.join(OUTPUT_DIR, "videos", output_name)# Create temporary directory for framestemp_dir = f"temp_frames_{timestamp}"os.makedirs(temp_dir, exist_ok=True)# Process framesframe_count = 0while True:ret, frame = cap.read()if not ret:break# Save frame temporarilytemp_frame_path = os.path.join(temp_dir, f"frame_{frame_count:06d}.png")cv2.imwrite(temp_frame_path, frame)# Super resolve framesuper_resolved_img = super_resolve_img(generator, temp_frame_path, output_path=None,downsample_threshold=720, crop_for_4x=True)# Save processed frameoutput_frame_path = os.path.join(temp_dir, f"output_{frame_count:06d}.png")save_image(super_resolved_img, output_frame_path)frame_count += 1progress = int((frame_count / total_frames) * 100)processing_status[task_id] = {"status": "processing", "progress": progress}print(f"Task {task_id}: Processed frame {frame_count}/{total_frames} ({progress}%)")cap.release()# Combine frames into video using ffmpegprint(f"Task {task_id}: Combining frames into video...")processing_status[task_id] = {"status": "encoding", "progress": 100}os.system(f"ffmpeg -framerate {fps} -i {temp_dir}/output_%06d.png -c:v libx264 -pix_fmt yuv420p {output_path}")# Clean upshutil.rmtree(temp_dir)processing_status[task_id] = {"status": "completed", "progress": 100, "output": output_path}add_to_history(video_path, output_path, model_name, "video")print(f"Task {task_id}: Completed! Output: {output_path}")except Exception as error:processing_status[task_id] = {"status": "error", "error": str(error)}print(f"Task {task_id}: Error - {error}")def video_queue_worker():"""Background worker to process video queue"""print("Video queue worker started...")while True:try:task = video_queue.get()if task is None: # Poison pill to stop workerbreaktask_id, video_path, model_name = taskprint(f"Starting task {task_id}...")process_video_frame_by_frame(video_path, model_name, task_id)except Exception as e:print(f"Worker error: {e}")finally:video_queue.task_done()def submit_video(video_path, model_name):"""Submit video to processing queue"""if video_path is None:return None, "β Please upload a video first"task_id = f"task_{int(time.time() * 1000)}"video_queue.put((task_id, video_path, model_name))processing_status[task_id] = {"status": "queued", "progress": 0}return None, f"β Video submitted to queue! Task ID: {task_id}\nCheck status in the monitoring section."def get_queue_status():"""Get current queue status"""status_text = "π **Queue Status**\n\n"status_text += f"Videos in queue: {video_queue.qsize()}\n\n"if processing_status:status_text += "**Active Tasks:**\n"for task_id, status in processing_status.items():status_text += f"\nπ¬ {task_id}:\n"status_text += f" Status: {status['status']}\n"status_text += f" Progress: {status.get('progress', 0)}%\n"if 'output' in status:status_text += f" Output: {status['output']}\n"if 'error' in status:status_text += f" Error: {status['error']}\n"else:status_text += "No active tasks"return status_textdef get_history_display():"""Get formatted history for display"""history = load_history()if not history:return "No history available"history_text = "π **Processing History**\n\n"for idx, record in enumerate(history[:50]): # Show last 50history_text += f"**{idx + 1}. {record['process_type'].upper()}** - {record['timestamp']}\n"history_text += f" Model: {record['model_name']}\n"history_text += f" Status: {record['status']}\n"history_text += f" Output: {record['output_path']}\n\n"return history_textdef clear_history():"""Clear all history"""if os.path.exists(HISTORY_FILE):os.remove(HISTORY_FILE)return "β History cleared!"if __name__ == '__main__':# Start background worker threadworker_thread = threading.Thread(target=video_queue_worker, daemon=True)worker_thread.start()MARKDOWN = \"""## <p style='text-align: center'> APISR: Anime Production Inspired Real-World Anime Super-Resolution (CVPR 2024) </p>2 unmodified linesAPISR aims at restoring and enhancing low-quality low-resolution **anime** images and video sources with various degradations from real-world scenarios.### β οΈ Note: Images with short side > 720px will be downsampled to 720px (e.g., 1920x1080 β 1280x720)### πΉ New: Video processing runs in background queue - you can close the browser and it continues!"""block = gr.Blocks().queue(max_size=10)with block:gr.Markdown(MARKDOWN)with gr.Tabs():# Tab 1: Image Processingwith gr.Tab("πΌοΈ Image Processing"):with gr.Row():with gr.Column(scale=2):input_image = gr.Image(type="filepath", label="Input Image")image_model = gr.Dropdown(["2xRRDB", "4xRRDB", "4xGRL", "4xDAT"],value="4xGRL",label="Model")image_btn = gr.Button("π Process Image", variant="primary")with gr.Column(scale=3):output_image = gr.Image(type="numpy", label="Output Image")image_status = gr.Textbox(label="Status", lines=2)with gr.Row():gr.Examples([["__assets__/lr_inputs/image-00277.png"],["__assets__/lr_inputs/image-00542.png"],["__assets__/lr_inputs/41.png"],["__assets__/lr_inputs/f91.jpg"],],[input_image],)image_btn.click(inference_image,inputs=[input_image, image_model],outputs=[output_image, image_status])# Tab 2: Video Processingwith gr.Tab("π¬ Video Processing"):gr.Markdown("""### Video Processing QueueVideos are processed in the background. You can submit multiple videos and close the browser - processing continues!""")with gr.Row():with gr.Column():input_video = gr.Video(label="Input Video")video_model = gr.Dropdown(["2xRRDB", "4xRRDB", "4xGRL", "4xDAT"],value="4xGRL",label="Model")video_btn = gr.Button("π€ Submit to Queue", variant="primary")video_status = gr.Textbox(label="Submission Status", lines=3)with gr.Column():gr.Markdown("### π Queue Monitor")queue_status = gr.Textbox(label="Queue Status", lines=15)refresh_btn = gr.Button("π Refresh Status")video_btn.click(submit_video,inputs=[input_video, video_model],outputs=[input_video, video_status])refresh_btn.click(get_queue_status,outputs=[queue_status])# Tab 3: Historywith gr.Tab("π History"):gr.Markdown("### Processing History")with gr.Row():refresh_history_btn = gr.Button("π Refresh History")clear_history_btn = gr.Button("ποΈ Clear History", variant="stop")history_display = gr.Textbox(label="History", lines=20)clear_status = gr.Textbox(label="Status", lines=1)refresh_history_btn.click(get_history_display,outputs=[history_display])clear_history_btn.click(clear_history,outputs=[clear_status])# Auto-load history on tab openblock.load(get_history_display, outputs=[history_display])block.launch(server_name="0.0.0.0", server_port=7860)
app.py+342-79