Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import skia
|
| 3 |
+
|
| 4 |
+
# Define the category dictionary
|
| 5 |
+
category_dict = {
|
| 6 |
+
0: "Algorithm",
|
| 7 |
+
1: "Caption",
|
| 8 |
+
2: "Equation",
|
| 9 |
+
3: "Figure",
|
| 10 |
+
4: "Footnote",
|
| 11 |
+
5: "List",
|
| 12 |
+
7: "Table",
|
| 13 |
+
8: "Text",
|
| 14 |
+
9: "Text-EQ",
|
| 15 |
+
10: "Title",
|
| 16 |
+
12: "PaperTitle",
|
| 17 |
+
13: "Code",
|
| 18 |
+
14: "Abstract"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Function to draw bounding box and label on the image using Skia
|
| 22 |
+
def draw_annotation(canvas, bbox, category, source_code):
|
| 23 |
+
paint = skia.Paint(Color=skia.ColorRED, Style=skia.Paint.kStroke_Style, StrokeWidth=2)
|
| 24 |
+
text_paint = skia.Paint(Color=skia.ColorRED, TextSize=12)
|
| 25 |
+
|
| 26 |
+
# Unpack the bounding box coordinates
|
| 27 |
+
x_min, y_min, x_max, y_max = bbox
|
| 28 |
+
|
| 29 |
+
# Draw the bounding box
|
| 30 |
+
canvas.drawRect(skia.Rect.MakeLTRB(x_min, y_min, x_max, y_max), paint)
|
| 31 |
+
|
| 32 |
+
# Draw the category label
|
| 33 |
+
label = f"{category_dict[category]}: {source_code}"
|
| 34 |
+
canvas.drawSimpleText(label, x_min, y_min - 10, skia.Font(None, 12), text_paint)
|
| 35 |
+
|
| 36 |
+
# Load the annotations from a JSON file
|
| 37 |
+
def load_annotations(file_path):
|
| 38 |
+
with open(file_path, 'r') as file:
|
| 39 |
+
annotations = json.load(file)
|
| 40 |
+
return annotations
|
| 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"
|
| 47 |
+
|
| 48 |
+
# Load the image using Skia
|
| 49 |
+
image = skia.Image.MakeFromEncoded(skia.Data.MakeFromFileName(image_path))
|
| 50 |
+
canvas = skia.Surface(image.width(), image.height()).getCanvas()
|
| 51 |
+
canvas.drawImage(image, 0, 0)
|
| 52 |
+
|
| 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)
|