Spaces:
Runtime error
Runtime error
improve user interaction
Browse filesFiles changed (3)
- app.py+130-177
- assets/box-instructions.movDeleted-3
- assets/point-instructions.movDeleted-3
48 unmodified lines49505152535455565758596061626364656667686970717273747576777816 unmodified lines95969798991001011021031041051061071081 unmodified line1101111121131141151161171181191201211221231241251261271281291301311321331341351 unmodified line1371381391401411421431441451461 unmodified line1481491501511521535 unmodified lines1591601611621631641651661671681697 unmodified lines17717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026124 unmodified lines2862872882892902912922932942952961 unmodified line29829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732822 unmodified lines3513523533543553563573583593603613623633643653663673686 unmodified lines3753763773783793803813823833843853863873882 unmodified lines39139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750848 unmodified lines# Descriptiontitle = "<center><strong><font size='8'>EdgeSAM<font></strong> <a href='https://github.com/chongzhou96/EdgeSAM'><font size='6'>[GitHub]</font></a> </center>"description_p = """ # Instructions for point mode [[Instructional video](https://huggingface.co/spaces/chongzhou/EdgeSAM/resolve/main/assets/point-instructions.mov)]1. Upload an image or click one of the provided examples.2. Select the point type.3. Click once or multiple times on the image to indicate the object of interest.4. Click Start to get the segmentation mask.5. The Clear button clears all the points.6. The Reset button resets both points and the image."""description_b = """ # Instructions for box mode [[Instructional video](https://huggingface.co/spaces/chongzhou/EdgeSAM/resolve/main/assets/box-instructions.mov)]1. Upload an image or click one of the provided examples.2. Click twice on the image (diagonal points of the box).3. Click Start to get the segmentation mask.4. The Clear button clears the box.5. The Reset button resets both the box and the image."""description_e = """ # Everything mode is NOT recommended.Since EdgeSAM follows the same encoder-decoder architecture as SAM, the everything mode will infer the decoder 32x32=1024 times, which is inefficient, thus a longer processing time is expected."""16 unmodified lines["assets/16.jpeg"]]default_example = examples[0]css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"global_points = []global_point_label = []global_box = []global_image = Nonedef reset():1 unmodified lineglobal global_point_labelglobal global_boxglobal global_imageglobal_points = []global_point_label = []global_box = []global_image = Nonereturn None, Nonedef reset_all():global global_pointsglobal global_point_labelglobal global_boxglobal global_imageglobal_points = []global_point_label = []global_box = []global_image = Noneif args.enable_everything_mode:return None, None, None, None, None, Noneelse:return None, None, None, Nonedef clear():1 unmodified lineglobal global_point_labelglobal global_boxglobal global_imageglobal_points = []global_point_label = []global_box = []return global_image, Nonedef on_image_upload(image, input_size=1024):1 unmodified lineglobal global_point_labelglobal global_boxglobal global_imageglobal_points = []global_point_label = []global_box = []5 unmodified linesnew_h = int(h * scale)image = image.resize((new_w, new_h))global_image = copy.deepcopy(image)print("Image changed")nd_image = np.array(global_image)predictor.set_image(nd_image)return image, Nonedef convert_box(xyxy):7 unmodified linesxyxy[1][1] = max_yreturn xyxydef get_points_with_draw(image, label, evt: gr.SelectData):global global_pointsglobal global_point_label# global global_imagex, y = evt.index[0], evt.index[1]# x = int(x * scale)# y = int(y * scale)point_radius, point_color = 10, (97, 217, 54) if label == "Positive" else (237, 34, 13)global_points.append([x, y])global_point_label.append(1 if label == "Positive" else 0)print(f'global_points: {global_points}')print(f'global_point_label: {global_point_label}')draw = ImageDraw.Draw(image)draw.ellipse([(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],fill=point_color,)return imagedef get_box_with_draw(image, evt: gr.SelectData):global global_box# global global_imagex, y = evt.index[0], evt.index[1]# x = float(x * scale)# y = float(y * scale)point_radius, point_color, box_outline = 5, (97, 217, 54), 5box_color = (0, 255, 0)if len(global_box) == 0:global_box.append([x, y])elif len(global_box) == 1:global_box.append([x, y])elif len(global_box) == 2:global_box = [[x, y]]print(f'global_box: {global_box}')draw = ImageDraw.Draw(image)draw.ellipse([(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],fill=point_color,)if len(global_box) == 2:global_box = convert_box(global_box)xy = (global_box[0][0], global_box[0][1], global_box[1][0], global_box[1][1])draw.rectangle(xy,outline=box_color,width=box_outline)return imagedef segment_with_points(image,input_size=1024,better_quality=False,withContours=True,use_retina=True,mask_random_color=False,):global global_pointsglobal global_point_labelglobal_points_np = np.array(global_points)global_point_label_np = np.array(global_point_label)if global_points_np.size == 0 and global_point_label_np.size == 0:print("No point selected")return image, imagenum_multimask_outputs = 4masks, scores, logits = predictor.predict(24 unmodified lineswithContours=withContours,)return image, segdef segment_with_box(image,input_size=1024,better_quality=False,withContours=True,1 unmodified linemask_random_color=False,):global global_boxglobal_box_np = np.array(global_box)if global_box_np.size < 4:print("No box selected")return image, imagemasks, scores, logits = predictor.predict(box=global_box_np,num_multimask_outputs=1,)annotations = masksseg = fast_process(annotations=annotations,image=image,device=device,scale=(1024 // input_size),better_quality=better_quality,mask_random_color=mask_random_color,bbox=None,use_retina=use_retina,withContours=withContours,)return image, segdef segment_everything(22 unmodified linesreturn segcond_img_p = gr.Image(label="Input with points", type="pil")cond_img_b = gr.Image(label="Input with box", type="pil")cond_img_e = gr.Image(label="Input (everything)", type="pil")segm_img_p = gr.Image(label="Segmented Image with points", interactive=False, type="pil")segm_img_b = gr.Image(label="Segmented Image with box", interactive=False, type="pil")segm_img_e = gr.Image(label="Segmented Everything", interactive=False, type="pil")if args.enable_everything_mode:all_outputs = [cond_img_p, cond_img_b, cond_img_e, segm_img_p, segm_img_b, segm_img_e]else:all_outputs = [cond_img_p, cond_img_b, segm_img_p, segm_img_b]with gr.Blocks(css=css, title="EdgeSAM") as demo:6 unmodified lines# Imageswith gr.Row(variant="panel"):with gr.Column(scale=1):cond_img_p.render()with gr.Column(scale=1):segm_img_p.render()# Submit & Clearwith gr.Row():with gr.Column():with gr.Row():add_or_remove = gr.Radio(["Positive", "Negative"],2 unmodified lines)with gr.Column():segment_btn_p = gr.Button("Start", variant="primary")clear_btn_p = gr.Button("Clear", variant="secondary")reset_btn_p = gr.Button("Reset", variant="secondary")gr.Markdown("Try some of the examples below ⬇️")gr.Examples(examples=examples,inputs=[cond_img_p],outputs=[cond_img_p, segm_img_p],examples_per_page=4,fn=on_image_upload,run_on_click=True)with gr.Column():# Descriptiongr.Markdown(description_p)with gr.Tab("Box mode") as tab_b:# Imageswith gr.Row(variant="panel"):with gr.Column(scale=1):cond_img_b.render()with gr.Column(scale=1):segm_img_b.render()# Submit & Clearwith gr.Row():with gr.Column():with gr.Row():with gr.Column():segment_btn_b = gr.Button("Start", variant="primary")clear_btn_b = gr.Button("Clear", variant="secondary")reset_btn_b = gr.Button("Reset", variant="secondary")gr.Markdown("Try some of the examples below ⬇️")gr.Examples(examples=examples,inputs=[cond_img_b],outputs=[cond_img_b, segm_img_b],examples_per_page=4,fn=on_image_upload,run_on_click=True)with gr.Column():# Descriptiongr.Markdown(description_b)if args.enable_everything_mode:with gr.Tab("Everything mode") as tab_e:# Imageswith gr.Row(variant="panel"):with gr.Column(scale=1):cond_img_e.render()with gr.Column(scale=1):segm_img_e.render()# Submit & Clearwith gr.Row():with gr.Column():with gr.Row():with gr.Column():segment_btn_e = gr.Button("Start", variant="primary")reset_btn_e = gr.Button("Reset", variant="secondary")gr.Markdown("Try some of the examples below ⬇️")gr.Examples(examples=examples,inputs=[cond_img_e],examples_per_page=4,)with gr.Column():# Descriptiongr.Markdown(description_e)with gr.Row():with gr.Column(scale=1):gr.Markdown("<center><img src='https://visitor-badge.laobi.icu/badge?page_id=chongzhou/edgesam' alt='visitors'></center>")cond_img_p.upload(on_image_upload, cond_img_p, [cond_img_p, segm_img_p])cond_img_p.select(get_points_with_draw, [cond_img_p, add_or_remove], cond_img_p)segment_btn_p.click(segment_with_points, inputs=[cond_img_p], outputs=[cond_img_p, segm_img_p])clear_btn_p.click(clear, outputs=[cond_img_p, segm_img_p])reset_btn_p.click(reset, outputs=[cond_img_p, segm_img_p])tab_p.select(fn=reset_all, outputs=all_outputs)cond_img_b.upload(on_image_upload, cond_img_b, [cond_img_b, segm_img_b])cond_img_b.select(get_box_with_draw, [cond_img_b], cond_img_b)segment_btn_b.click(segment_with_box, inputs=[cond_img_b], outputs=[cond_img_b, segm_img_b])clear_btn_b.click(clear, outputs=[cond_img_b, segm_img_b])reset_btn_b.click(reset, outputs=[cond_img_b, segm_img_b])tab_b.select(fn=reset_all, outputs=all_outputs)if args.enable_everything_mode:segment_btn_e.click(segment_everything, inputs=[cond_img_e], outputs=segm_img_e)reset_btn_e.click(reset, outputs=[cond_img_e, segm_img_e])tab_e.select(fn=reset_all, outputs=all_outputs)demo.queue()48 unmodified lines495051525354555657585960616263646566676869707172737475767778798016 unmodified lines9798991001011021031041051061071081091 unmodified line1111121131141151161171181191201211221231241251261271281291301311321331341351361371381391 unmodified line1411421431441451461471481491501511521 unmodified line1541551561571581591605 unmodified lines1661671681691701711721731741751761777 unmodified lines18518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022124 unmodified lines2462472482492502512522532542552561 unmodified line25825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631722 unmodified lines3403413423433443453463473483493503513523536 unmodified lines3603613623633643653663672 unmodified lines37037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046148 unmodified lines# Descriptiontitle = "<center><strong><font size='8'>EdgeSAM<font></strong> <a href='https://github.com/chongzhou96/EdgeSAM'><font size='6'>[GitHub]</font></a> </center>"description_p = """ # Instructions for point mode1. Upload an image or click one of the provided examples.2. Select the point type.3. Click once or multiple times on the image to indicate the object of interest.4. The Clear button clears all the points.5. The Reset button resets both points and the image."""description_b = """ # Instructions for box mode1. Upload an image or click one of the provided examples.2. Click twice on the image (diagonal points of the box).3. The Clear button clears the box.4. The Reset button resets both the box and the image."""description_e = """ # Everything mode is NOT recommended.Since EdgeSAM follows the same encoder-decoder architecture as SAM, the everything mode will infer the decoder 32x32=1024 times, which is inefficient, thus a longer processing time is expected.1. Upload an image or click one of the provided examples.2. Click Start to get the segmentation mask.3. The Reset button resets the image and masks."""16 unmodified lines["assets/16.jpeg"]]css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"global_points = []global_point_label = []global_box = []global_image = Noneglobal_image_with_prompt = Nonedef reset():1 unmodified lineglobal global_point_labelglobal global_boxglobal global_imageglobal global_image_with_promptglobal_points = []global_point_label = []global_box = []global_image = Noneglobal_image_with_prompt = Nonereturn Nonedef reset_all():global global_pointsglobal global_point_labelglobal global_boxglobal global_imageglobal global_image_with_promptglobal_points = []global_point_label = []global_box = []global_image = Noneglobal_image_with_prompt = Noneif args.enable_everything_mode:return None, None, Noneelse:return None, Nonedef clear():1 unmodified lineglobal global_point_labelglobal global_boxglobal global_imageglobal global_image_with_promptglobal_points = []global_point_label = []global_box = []global_image_with_prompt = copy.deepcopy(global_image)return global_imagedef on_image_upload(image, input_size=1024):1 unmodified lineglobal global_point_labelglobal global_boxglobal global_imageglobal global_image_with_promptglobal_points = []global_point_label = []global_box = []5 unmodified linesnew_h = int(h * scale)image = image.resize((new_w, new_h))global_image = copy.deepcopy(image)global_image_with_prompt = copy.deepcopy(image)print("Image changed")nd_image = np.array(global_image)predictor.set_image(nd_image)return imagedef convert_box(xyxy):7 unmodified linesxyxy[1][1] = max_yreturn xyxydef segment_with_points(label,evt: gr.SelectData,input_size=1024,better_quality=False,withContours=True,use_retina=True,mask_random_color=False,):global global_pointsglobal global_point_labelglobal global_image_with_promptx, y = evt.index[0], evt.index[1]point_radius, point_color = 5, (97, 217, 54) if label == "Positive" else (237, 34, 13)global_points.append([x, y])global_point_label.append(1 if label == "Positive" else 0)print(f'global_points: {global_points}')print(f'global_point_label: {global_point_label}')draw = ImageDraw.Draw(global_image_with_prompt)draw.ellipse([(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],fill=point_color,)image = global_image_with_promptglobal_points_np = np.array(global_points)global_point_label_np = np.array(global_point_label)num_multimask_outputs = 4masks, scores, logits = predictor.predict(24 unmodified lineswithContours=withContours,)return segdef segment_with_box(evt: gr.SelectData,input_size=1024,better_quality=False,withContours=True,1 unmodified linemask_random_color=False,):global global_boxglobal global_imageglobal global_image_with_promptx, y = evt.index[0], evt.index[1]point_radius, point_color, box_outline = 5, (97, 217, 54), 5box_color = (0, 255, 0)if len(global_box) == 0:global_box.append([x, y])elif len(global_box) == 1:global_box.append([x, y])elif len(global_box) == 2:global_image_with_prompt = copy.deepcopy(global_image)global_box = [[x, y]]print(f'global_box: {global_box}')draw = ImageDraw.Draw(global_image_with_prompt)draw.ellipse([(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],fill=point_color,)image = global_image_with_promptif len(global_box) == 2:global_box = convert_box(global_box)xy = (global_box[0][0], global_box[0][1], global_box[1][0], global_box[1][1])draw.rectangle(xy,outline=box_color,width=box_outline)global_box_np = np.array(global_box)masks, scores, logits = predictor.predict(box=global_box_np,num_multimask_outputs=1,)annotations = masksseg = fast_process(annotations=annotations,image=image,device=device,scale=(1024 // input_size),better_quality=better_quality,mask_random_color=mask_random_color,bbox=None,use_retina=use_retina,withContours=withContours,)return segreturn imagedef segment_everything(22 unmodified linesreturn segimg_p = gr.Image(label="Input with points", type="pil")img_b = gr.Image(label="Input with box", type="pil")img_e = gr.Image(label="Input (everything)", type="pil")if args.enable_everything_mode:all_outputs = [img_p, img_b, img_e]else:all_outputs = [img_p, img_b]with gr.Blocks(css=css, title="EdgeSAM") as demo:6 unmodified lines# Imageswith gr.Row(variant="panel"):with gr.Column(scale=1):img_p.render()with gr.Column(scale=1):with gr.Row():add_or_remove = gr.Radio(["Positive", "Negative"],2 unmodified lines)with gr.Column():clear_btn_p = gr.Button("Clear", variant="secondary")reset_btn_p = gr.Button("Reset", variant="secondary")with gr.Row():gr.Markdown(description_p)with gr.Row():with gr.Column():gr.Markdown("Try some of the examples below ⬇️")gr.Examples(examples=examples,inputs=[img_p],outputs=[img_p],examples_per_page=8,fn=on_image_upload,run_on_click=True)with gr.Tab("Box mode") as tab_b:# Imageswith gr.Row(variant="panel"):with gr.Column(scale=1):img_b.render()with gr.Row():with gr.Column():clear_btn_b = gr.Button("Clear", variant="secondary")reset_btn_b = gr.Button("Reset", variant="secondary")gr.Markdown(description_b)with gr.Row():with gr.Column():gr.Markdown("Try some of the examples below ⬇️")gr.Examples(examples=examples,inputs=[img_b],outputs=[img_b],examples_per_page=8,fn=on_image_upload,run_on_click=True)if args.enable_everything_mode:with gr.Tab("Everything mode") as tab_e:# Imageswith gr.Row(variant="panel"):with gr.Column(scale=1):img_e.render()with gr.Column(scale=1):with gr.Row():with gr.Column():segment_btn_e = gr.Button("Start", variant="primary")reset_btn_e = gr.Button("Reset", variant="secondary")gr.Markdown(description_e)# Submit & Clearwith gr.Row():with gr.Column():gr.Markdown("Try some of the examples below ⬇️")gr.Examples(examples=examples,inputs=[img_e],examples_per_page=8,)with gr.Row():with gr.Column(scale=1):gr.Markdown("<center><img src='https://visitor-badge.laobi.icu/badge?page_id=chongzhou/edgesam' alt='visitors'></center>")img_p.upload(on_image_upload, img_p, [img_p])img_p.select(segment_with_points, [add_or_remove], img_p)clear_btn_p.click(clear, outputs=[img_p])reset_btn_p.click(reset, outputs=[img_p])tab_p.select(fn=reset_all, outputs=all_outputs)img_b.upload(on_image_upload, img_b, [img_b])img_b.select(segment_with_box, outputs=[img_b])clear_btn_b.click(clear, outputs=[img_b])reset_btn_b.click(reset, outputs=[img_b])tab_b.select(fn=reset_all, outputs=all_outputs)if args.enable_everything_mode:segment_btn_e.click(segment_everything, inputs=[img_e], outputs=img_e)reset_btn_e.click(reset, outputs=[img_e])tab_e.select(fn=reset_all, outputs=all_outputs)demo.queue()
123version https://git-lfs.github.com/spec/v1oid sha256:b6d85a91a61f63f5636f42832fb8751b36cdce92cefefdcef05816f1c931c00asize 8007816123version https://git-lfs.github.com/spec/v1oid sha256:fbe4b4f30563fa059e600bb0dff599c2666821e7a5e5f799a0ad2d82f6895ebdsize 20135690app.py+130-177
assets/box-instructions.movDeleted-3
assets/point-instructions.movDeleted-3