stzhao commited on
Commit
5a163b8
·
verified ·
1 Parent(s): 59ab742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import json
2
  import skia
 
3
 
4
  # Define the category dictionary
5
  category_dict = {
@@ -41,6 +42,7 @@ def load_annotations(file_path):
41
 
42
  # Main function to render annotations onto images using Skia
43
  def render_annotations(annotations, image_dir):
 
44
  for annotation in annotations:
45
  page_index = annotation['page_index']
46
  image_path = f"{image_dir}/page_{page_index:04d}.jpg"
@@ -53,16 +55,30 @@ def render_annotations(annotations, image_dir):
53
  # Draw the annotation on the image
54
  draw_annotation(canvas, annotation['bbox'], annotation['category'], annotation['source_code'])
55
 
56
- # Save the annotated image
57
- output_path = f"{image_dir}/annotated_page_{page_index:04d}.jpg"
58
  surface = canvas.getSurface()
59
- surface.makeImageSnapshot().save(output_path, skia.kJPEG_Codec)
60
- print(f"Saved annotated image to {output_path}")
61
-
62
- # Example usage
63
- if __name__ == "__main__":
64
- annotations_file = "annotations.json" # Path to your annotations JSON file
65
- image_directory = "images" # Directory containing the images
66
 
 
 
 
 
67
  annotations = load_annotations(annotations_file)
68
- render_annotations(annotations, image_directory)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import json
2
  import skia
3
+ import gradio as gr
4
 
5
  # Define the category dictionary
6
  category_dict = {
 
42
 
43
  # Main function to render annotations onto images using Skia
44
  def render_annotations(annotations, image_dir):
45
+ annotated_images = []
46
  for annotation in annotations:
47
  page_index = annotation['page_index']
48
  image_path = f"{image_dir}/page_{page_index:04d}.jpg"
 
55
  # Draw the annotation on the image
56
  draw_annotation(canvas, annotation['bbox'], annotation['category'], annotation['source_code'])
57
 
58
+ # Save the annotated image to a buffer
 
59
  surface = canvas.getSurface()
60
+ image_buffer = surface.makeImageSnapshot().encode(skia.kPNG_Codec)
61
+ annotated_images.append((image_buffer, f"Page {page_index}"))
 
 
 
 
 
62
 
63
+ return annotated_images
64
+
65
+ # Gradio interface function
66
+ def gradio_interface(annotations_file, image_dir):
67
  annotations = load_annotations(annotations_file)
68
+ annotated_images = render_annotations(annotations, image_dir)
69
+ return annotated_images
70
+
71
+ # Create Gradio interface
72
+ iface = gr.Interface(
73
+ fn=gradio_interface,
74
+ inputs=[
75
+ gr.inputs.File(label="Upload Annotations JSON File"),
76
+ gr.inputs.Textbox(label="Image Directory")
77
+ ],
78
+ outputs=[gr.outputs.Carousel(gr.outputs.Image(type="file"), label="Annotated Images")],
79
+ title="Paper Annotation Renderer",
80
+ description="Upload an annotations JSON file and specify the image directory to render annotations onto the images."
81
+ )
82
+
83
+ # Launch the Gradio interface
84
+ iface.launch()