ginipick commited on
Commit
d6f5e9d
·
verified ·
1 Parent(s): 9bf26ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -129
app.py CHANGED
@@ -259,202 +259,150 @@ button.primary:hover {
259
  background: #1976D2;
260
  }
261
  """
262
- # Gradio UI 부분 수정
263
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
264
  gr.HTML("""
265
  <div class="main-title">
266
- <h1>🎨 Advanced Image Object Extractor</h1>
267
  <p>Extract objects from images using text prompts or bounding boxes</p>
268
  </div>
269
  """)
270
 
271
  with gr.Tabs() as tabs:
272
  # Text 탭
273
- with gr.Tab("Extract by Text", id="tab_prompt"):
274
- with gr.Row(equal_height=True):
275
- with gr.Column(scale=1, min_width=400):
276
- gr.HTML("<h3>Input Section</h3>")
277
  input_image = gr.Image(
278
  type="pil",
279
  label="Upload Image",
280
- interactive=True
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  )
282
- with gr.Group():
283
- text_prompt = gr.Textbox(
284
- label="Object to Extract",
285
- placeholder="Enter what you want to extract...",
286
- interactive=True
287
- )
288
- background_prompt = gr.Textbox(
289
- label="Background Generation Prompt (optional)",
290
- placeholder="Describe the background you want...",
291
- interactive=True
292
- )
293
  process_btn = gr.Button(
294
- "Process Image",
295
- variant="primary"
 
296
  )
297
 
298
- with gr.Column(scale=1, min_width=400):
299
- gr.HTML("<h3>Output Section</h3>")
300
- output_image = ImageSlider(
301
- label="Results Preview",
302
- show_download_button=False
303
  )
304
  download_btn = gr.DownloadButton(
305
- "Download Result"
 
306
  )
307
 
308
- # Text 탭 Examples
309
  with gr.Accordion("Examples", open=False):
310
- text_examples = [
311
- ["examples/text.jpg", "text", "white background"],
312
- ["examples/black-lamp.jpg", "black lamp", "minimalist interior"]
313
- ]
314
  gr.Examples(
315
- examples=text_examples,
316
- inputs=[input_image, text_prompt, background_prompt],
317
- outputs=[output_image, download_btn],
 
 
 
318
  fn=process_prompt,
319
  cache_examples=True
320
  )
321
 
322
- # Bounding Box 탭
323
- with gr.Tab("Extract by Box", id="tab_bb"):
324
- with gr.Row(equal_height=True):
325
- with gr.Column(scale=1, min_width=400):
326
- gr.HTML("<h3>Input Section</h3>")
327
  box_annotator = image_annotator(
328
  image_type="pil",
329
- disable_edit_boxes=False, # 편집 가능하도록 변경
330
  show_download_button=False,
331
  show_share_button=False,
332
  single_box=True,
333
  label="Draw Box Around Object",
334
- interactive=True
 
335
  )
336
- box_process_btn = gr.Button(
337
  "Extract Selection",
338
- variant="primary"
 
339
  )
340
 
341
- with gr.Column(scale=1, min_width=400):
342
- gr.HTML("<h3>Output Section</h3>")
343
- box_output_image = ImageSlider(
344
- label="Results Preview",
345
- show_download_button=False
346
  )
347
- box_download_btn = gr.DownloadButton(
348
- "Download Result"
 
349
  )
350
 
351
- # Bounding Box Examples
352
  with gr.Accordion("Examples", open=False):
353
- box_examples = [
354
- {
355
- "image": "examples/text.jpg",
356
- "boxes": [{"xmin": 51, "ymin": 511, "xmax": 639, "ymax": 1255}]
357
- },
358
- {
359
- "image": "examples/black-lamp.jpg",
360
- "boxes": [{"xmin": 88, "ymin": 148, "xmax": 700, "ymax": 1414}]
361
- }
362
- ]
363
  gr.Examples(
364
- examples=box_examples,
 
 
 
 
 
 
 
 
 
365
  inputs=[box_annotator],
366
- outputs=[box_output_image, box_download_btn],
367
  fn=process_bbox,
368
  cache_examples=True
369
  )
370
 
371
  # Event handlers
372
- def update_button_state(img, prompt):
373
  return gr.Button.update(interactive=bool(img and prompt))
374
 
375
- # Text 이벤트
376
  input_image.change(
377
- fn=update_button_state,
378
  inputs=[input_image, text_prompt],
379
- outputs=[process_btn]
380
  )
381
  text_prompt.change(
382
- fn=update_button_state,
383
  inputs=[input_image, text_prompt],
384
- outputs=[process_btn]
385
  )
386
  process_btn.click(
387
  fn=process_prompt,
388
- inputs=[input_image, text_prompt, background_prompt],
389
- outputs=[output_image, download_btn]
390
  )
391
 
392
- # Bounding Box 이벤트
393
  box_annotator.change(
394
  fn=lambda x: gr.Button.update(interactive=bool(x)),
395
  inputs=[box_annotator],
396
- outputs=[box_process_btn]
397
  )
398
- box_process_btn.click(
399
  fn=process_bbox,
400
  inputs=[box_annotator],
401
- outputs=[box_output_image, box_download_btn]
402
  )
403
 
404
- # CSS 스타일 업데이트
405
- css = """
406
- footer {display: none}
407
- .main-title {
408
- text-align: center;
409
- margin: 2em 0;
410
- padding: 1em;
411
- background: #f7f7f7;
412
- border-radius: 10px;
413
- }
414
- .main-title h1 {
415
- color: #2196F3;
416
- font-size: 2.5em;
417
- margin-bottom: 0.5em;
418
- }
419
- .main-title p {
420
- color: #666;
421
- font-size: 1.2em;
422
- }
423
- .container {
424
- max-width: 1200px;
425
- margin: auto;
426
- padding: 20px;
427
- }
428
- .tabs {
429
- margin-top: 1em;
430
- }
431
- .input-group {
432
- background: white;
433
- padding: 1em;
434
- border-radius: 8px;
435
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
436
- }
437
- .output-group {
438
- background: white;
439
- padding: 1em;
440
- border-radius: 8px;
441
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
442
- }
443
- button.primary {
444
- background: #2196F3;
445
- border: none;
446
- color: white;
447
- padding: 0.5em 1em;
448
- border-radius: 4px;
449
- cursor: pointer;
450
- transition: background 0.3s ease;
451
- }
452
- button.primary:hover {
453
- background: #1976D2;
454
- }
455
- """
456
-
457
- # Launch settings
458
  demo.queue(max_size=30, api_open=False)
459
  demo.launch(
460
  show_api=False,
 
259
  background: #1976D2;
260
  }
261
  """
262
+
263
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
264
  gr.HTML("""
265
  <div class="main-title">
266
+ <h1>🎨 Image Object Extractor</h1>
267
  <p>Extract objects from images using text prompts or bounding boxes</p>
268
  </div>
269
  """)
270
 
271
  with gr.Tabs() as tabs:
272
  # Text 탭
273
+ with gr.Tab(label="Extract by Text") as text_tab:
274
+ with gr.Row():
275
+ with gr.Column():
 
276
  input_image = gr.Image(
277
  type="pil",
278
  label="Upload Image",
279
+ interactive=True,
280
+ elem_id="input_image"
281
+ )
282
+ text_prompt = gr.Textbox(
283
+ label="Object to Extract",
284
+ placeholder="Enter what you want to extract...",
285
+ interactive=True,
286
+ elem_id="text_prompt"
287
+ )
288
+ bg_prompt = gr.Textbox(
289
+ label="Background Prompt (optional)",
290
+ placeholder="Describe the background...",
291
+ interactive=True,
292
+ elem_id="bg_prompt"
293
  )
 
 
 
 
 
 
 
 
 
 
 
294
  process_btn = gr.Button(
295
+ "Process",
296
+ variant="primary",
297
+ elem_id="process_btn"
298
  )
299
 
300
+ with gr.Column():
301
+ output_display = ImageSlider(
302
+ label="Results",
303
+ show_download_button=False,
304
+ elem_id="output_display"
305
  )
306
  download_btn = gr.DownloadButton(
307
+ "Download Result",
308
+ elem_id="download_btn"
309
  )
310
 
 
311
  with gr.Accordion("Examples", open=False):
 
 
 
 
312
  gr.Examples(
313
+ examples=[
314
+ ["examples/text.jpg", "text", "white background"],
315
+ ["examples/black-lamp.jpg", "black lamp", "minimalist interior"]
316
+ ],
317
+ inputs=[input_image, text_prompt, bg_prompt],
318
+ outputs=[output_display, download_btn],
319
  fn=process_prompt,
320
  cache_examples=True
321
  )
322
 
323
+ # Box 탭
324
+ with gr.Tab(label="📏 Extract by Box") as box_tab:
325
+ with gr.Row():
326
+ with gr.Column():
 
327
  box_annotator = image_annotator(
328
  image_type="pil",
329
+ disable_edit_boxes=False,
330
  show_download_button=False,
331
  show_share_button=False,
332
  single_box=True,
333
  label="Draw Box Around Object",
334
+ interactive=True,
335
+ elem_id="box_annotator"
336
  )
337
+ box_btn = gr.Button(
338
  "Extract Selection",
339
+ variant="primary",
340
+ elem_id="box_btn"
341
  )
342
 
343
+ with gr.Column():
344
+ box_output = ImageSlider(
345
+ label="Results",
346
+ show_download_button=False,
347
+ elem_id="box_output"
348
  )
349
+ box_download = gr.DownloadButton(
350
+ "Download Result",
351
+ elem_id="box_download"
352
  )
353
 
 
354
  with gr.Accordion("Examples", open=False):
 
 
 
 
 
 
 
 
 
 
355
  gr.Examples(
356
+ examples=[
357
+ {
358
+ "image": "examples/text.jpg",
359
+ "boxes": [{"xmin": 51, "ymin": 511, "xmax": 639, "ymax": 1255}]
360
+ },
361
+ {
362
+ "image": "examples/black-lamp.jpg",
363
+ "boxes": [{"xmin": 88, "ymin": 148, "xmax": 700, "ymax": 1414}]
364
+ }
365
+ ],
366
  inputs=[box_annotator],
367
+ outputs=[box_output, box_download],
368
  fn=process_bbox,
369
  cache_examples=True
370
  )
371
 
372
  # Event handlers
373
+ def update_process_button(img, prompt):
374
  return gr.Button.update(interactive=bool(img and prompt))
375
 
376
+ # Text tab events
377
  input_image.change(
378
+ fn=update_process_button,
379
  inputs=[input_image, text_prompt],
380
+ outputs=process_btn
381
  )
382
  text_prompt.change(
383
+ fn=update_process_button,
384
  inputs=[input_image, text_prompt],
385
+ outputs=process_btn
386
  )
387
  process_btn.click(
388
  fn=process_prompt,
389
+ inputs=[input_image, text_prompt, bg_prompt],
390
+ outputs=[output_display, download_btn]
391
  )
392
 
393
+ # Box tab events
394
  box_annotator.change(
395
  fn=lambda x: gr.Button.update(interactive=bool(x)),
396
  inputs=[box_annotator],
397
+ outputs=box_btn
398
  )
399
+ box_btn.click(
400
  fn=process_bbox,
401
  inputs=[box_annotator],
402
+ outputs=[box_output, box_download]
403
  )
404
 
405
+ # Launch the app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406
  demo.queue(max_size=30, api_open=False)
407
  demo.launch(
408
  show_api=False,