-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Dockerfile): update base image to slim and add build args for ve…
…rsioning feat(server): implement server functionalities with routers for health, detect, and search feat(pyproject.toml): add new dependencies for image processing, settings management, and testing feat(server): implement object detection and classification models for image analysis and search
- Loading branch information
Showing
16 changed files
with
796 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from fastapi import APIRouter, HTTPException | ||
import base64 | ||
|
||
from utils.scan import ( | ||
align_crop, | ||
align_inputs, | ||
detect_markers, | ||
detect_qr, | ||
extract_data, | ||
highlight, | ||
) | ||
|
||
router = APIRouter( | ||
prefix="/scan", | ||
tags=["scan"], | ||
dependencies=[], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.post("/") | ||
async def scan(images: list[str]): | ||
meta_data = [] | ||
cropped_images = [] | ||
for image in images: | ||
image = base64.b64decode(images[0].split(",")[1]) | ||
|
||
markers = detect_markers(image) | ||
cropped_image = align_crop(image, markers) | ||
cropped_images.append(cropped_image) | ||
meta_data.append(detect_qr(cropped_image)) | ||
|
||
if not all(item["scale"] == meta_data[0]["scale"] for item in meta_data): | ||
raise HTTPException(status_code=409, detail="Pages are not of a same scale") | ||
|
||
total_choice_indexes = set() | ||
for data in meta_data: | ||
start = data["choice"]["start"] | ||
count = data["choice"]["count"] | ||
|
||
choice_indexes = list(range(start, start + count)) | ||
total_choice_indexes.update(choice_indexes) | ||
|
||
if not set(range(1, meta_data[0]["choice"]["total"])).issubset( | ||
total_choice_indexes | ||
): | ||
raise HTTPException(status_code=400, detail="Insufficient number of pages") | ||
|
||
highlights = [] | ||
choices = [] | ||
for index, cropped_image in enumerate(cropped_images): | ||
# print(meta_data) | ||
option_count = meta_data[index]["option"] | ||
start = meta_data[index]["choice"]["start"] | ||
choice_count = meta_data[index]["choice"]["count"] | ||
inputs = align_inputs(cropped_image, option_count, start, choice_count) | ||
# print(inputs) | ||
choices.extend(extract_data(cropped_image, inputs)) | ||
highlights.append(highlight(cropped_image, option_count, inputs, choices)) | ||
|
||
# print(choices) | ||
return { | ||
"data": {"name": meta_data[0]["scale"], "choices": choices}, | ||
"highlights": highlights, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import numpy as np | ||
import cv2 | ||
|
||
|
||
def is_circle_inside(circle_center): | ||
# from markers 3,5,11,9 | ||
boundary = [ | ||
[70.0, 390.5], | ||
[2306.0, 390.5], | ||
[2306.0, 3294.0], | ||
[70.0, 3294.0], | ||
] | ||
|
||
x, y = circle_center | ||
x_min, y_min = boundary[0] | ||
x_max, y_max = boundary[2] | ||
|
||
if x_min <= x <= x_max and y_min <= y <= y_max: | ||
return True | ||
else: | ||
return False | ||
|
||
|
||
def choice_generator(option, index, total): | ||
factor = 4 | ||
index = index - 1 | ||
unit = 15 | ||
x = 55 | ||
y = 100 | ||
|
||
while index < total: | ||
if index % 40 == 0 and index != 0: | ||
x += 110 | ||
y = 100 | ||
elif index % 5 == 0 and index != 0: | ||
y += 15 | ||
|
||
y += unit | ||
|
||
choices = None | ||
if option == 2: | ||
choices = [ | ||
{"value": 1, "chord": [(x) * factor, (y) * factor]}, | ||
{"value": 0, "chord": [(x + unit) * factor, (y) * factor]}, | ||
] | ||
elif option == 5: | ||
choices = [ | ||
{"value": 0, "chord": [(x) * factor, (y) * factor]}, | ||
{"value": 1, "chord": [(x + 1 * unit) * factor, (y) * factor]}, | ||
{"value": 2, "chord": [(x + 2 * unit) * factor, (y) * factor]}, | ||
{"value": 3, "chord": [(x + 3 * unit) * factor, (y) * factor]}, | ||
{"value": 4, "chord": [(x + 4 * unit) * factor, (y) * factor]}, | ||
] | ||
|
||
yield {"index": index + 1, "choices": choices} | ||
|
||
index += 1 | ||
|
||
|
||
def calculate_bw_ratio(image): | ||
# Threshold the image to get binary image with white pixels | ||
_, binary = cv2.threshold(image, 250, 255, cv2.THRESH_BINARY) | ||
|
||
# Count the white pixels | ||
num_white_pixels = np.count_nonzero(binary == 255) | ||
|
||
# Calculate the ratio of white pixels to total pixels | ||
height, width = binary.shape | ||
num_pixels = width * height | ||
white_ratio = num_white_pixels / num_pixels | ||
|
||
return white_ratio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.