Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546import gradio as grfrom transformers import DPTFeatureExtractor, DPTForDepthEstimationimport torchimport numpy as npfrom PIL import Imagetorch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")def process_image(image):# prepare image for the modelencoding = feature_extractor(image, return_tensors="pt")# forward passwith torch.no_grad():outputs = model(**encoding)predicted_depth = outputs.predicted_depth# interpolate to original sizeprediction = torch.nn.functional.interpolate(predicted_depth.unsqueeze(1),size=image.size[::-1],mode="bicubic",align_corners=False,).squeeze()output = prediction.cpu().numpy()formatted = (output * 255 / np.max(output)).astype('uint8')img = Image.fromarray(formatted)return imgreturn resulttitle = "Demo: zero-shot depth estimation with DPT"description = "Demo for Intel's DPT, a Dense Prediction Transformer for state-of-the-art dense prediction tasks such as semantic segmentation and depth estimation."examples =[['cats.jpg']]iface = gr.Interface(fn=process_image,inputs=gr.inputs.Image(type="pil"),outputs=gr.outputs.Image(type="pil", label="predicted depth"),title=title,description=description,examples=examples,enable_queue=True)iface.launch(debug=True)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394from doctest import Exampleimport gradio as grfrom transformers import DPTImageProcessor, DPTForDepthEstimationimport torchimport numpy as npfrom PIL import Image, ImageOpsfrom pathlib import Pathimport globfrom autostereogram.converter import StereogramConverterfrom datetime import datetimeimport timeimport tempfilefeature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-large")model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")stereo_converter = StereogramConverter()def process_image(image_path):print("\n\n\n")print("Processing image:", image_path)last_time = time.time()image_raw = Image.open(Path(image_path))image = image_raw.resize((1280, int(1280 * image_raw.size[1] / image_raw.size[0])),Image.Resampling.LANCZOS,)# prepare image for the modelencoding = feature_extractor(image, return_tensors="pt")# forward passwith torch.no_grad():outputs = model(**encoding)predicted_depth = outputs.predicted_depth# interpolate to original sizeprediction = torch.nn.functional.interpolate(predicted_depth.unsqueeze(1),size=image.size[::-1],mode="bicubic",align_corners=False,).squeeze()output = prediction.cpu().numpy()depth_image = (output * 255 / np.max(output)).astype("uint8")depth_image_padded = np.array(ImageOps.pad(Image.fromarray(depth_image), (1280, 720)))return depth_image_paddedexamples_images = [[f] for f in sorted(glob.glob("examples/*.jpg"))]with gr.Blocks() as blocks:gr.Markdown("""## Depth Image to Autostereogram (Magic Eye)This demo is a variation from the original [DPT Demo](https://huggingface.co/spaces/nielsr/dpt-depth-estimation).Zero-shot depth estimation from an image, then it uses [pystereogram](https://github.com/yxiao1996/pystereogram)to generate the autostereogram (Magic Eye)<base target="_blank">""")with gr.Row():with gr.Column():input_image = gr.Image(type="filepath", label="Input Image")button = gr.Button("Predict")with gr.Column():predicted_depth = gr.Image(label="Predicted Depth", type="pil")with gr.Row():autostereogram = gr.Image(label="Autostereogram", type="pil")with gr.Row():with gr.Column():file_download = gr.File(label="Download Image")with gr.Row():gr.Examples(examples=examples_images,fn=process_image,inputs=[input_image],outputs=predicted_depth,cache_examples=True,)button.click(fn=process_image,inputs=[input_image],outputs=predicted_depth,)blocks.launch(debug=True)
app.py+81-33