| import gradio as gr | |
| import torch | |
| import torchvision.transforms as transforms | |
| from PIL import Image | |
| import OS | |
| file_urls = [ | |
| "https://www.bing.com/images/search?view=detailV2&ccid=YaFiK%2bN6&id=D84622E2396A39F168D279F32AC31F05096187AB&thid=OIP.YaFiK-N6iDdJR6B6DMBHpgHaFj&mediaurl=https%3a%2f%2fwww.practicalcaravan.com%2fwp-content%2fuploads%2f2016%2f03%2f5907569-scaled.jpg&exph=1921&expw=2560&q=audi+a4+car+image&simid=608053806389945942&FORM=IRPRST&ck=7DDB4BC7AA27F8E3EDA4433E669D3CC4&selectedIndex=6&ajaxhist=0&ajaxserp=0","https://www.bing.com/images/search?view=detailV2&ccid=CHONQxwQ&id=B8BCD1A5420658017C772CF149AFB7D24F2F8322&thid=OIP.CHONQxwQrclsFp-VXh4aOQHaFD&mediaurl=https%3a%2f%2fs3-eu-west-1.amazonaws.com%2feurekar-v2%2fuploads%2fimages%2foriginal%2fa4salfront.jpg&exph=1025&expw=1500&q=audi+a4+car+image&simid=608024308599848180&FORM=IRPRST&ck=3A2EA226332024ECB13B2F27682C15CA&selectedIndex=3&ajaxhist=0&ajaxserp=0" | |
| ] | |
| def download_file(url, save_name): | |
| url = url | |
| if not os.path.exists(save_name): | |
| file = requests.get(url) | |
| open(save_name, 'wb').write(file.content) | |
| for i, url in enumerate(file_urls): | |
| if 'mp4' in file_urls[i]: | |
| download_file( | |
| file_urls[i], | |
| f"video.mp4" | |
| ) | |
| else: | |
| download_file( | |
| file_urls[i], | |
| f"image_{i}.jpg" | |
| ) | |
| model = 'cifar_net.pth' | |
| path = [['image_0.jpg'], ['image_1.jpg']] | |
| video_path = [['video.mp4']] | |
| def show_preds_image(image_path): | |
| image = cv2.imread(image_path) | |
| outputs = model.predict(source=image_path) | |
| results = outputs[0].cpu().numpy() | |
| for i, det in enumerate(results.boxes.xyxy): | |
| cv2.rectangle( | |
| image, | |
| (int(det[0]), int(det[1])), | |
| (int(det[2]), int(det[3])), | |
| color=(0, 0, 255), | |
| thickness=2, | |
| lineType=cv2.LINE_AA | |
| ) | |
| return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| inputs_image = [ | |
| gr.components.Image(type="filepath", label="Input Image"), | |
| ] | |
| outputs_image = [ | |
| gr.components.Image(type="numpy", label="Output Image"), | |
| ] | |
| interface_image = gr.Interface( | |
| fn=show_preds_image, | |
| inputs=inputs_image, | |
| outputs=outputs_image, | |
| title="Car detector", | |
| examples=path, | |
| cache_examples=False, | |
| ) | |
| gr.TabbedInterface( | |
| [interface_image], | |
| tab_names=['Image inference'] | |
| ).queue().launch() |