| import { BASE_URL } from "@/utils/constants"; |
| import type { ImageResult, AngleLabel } from "@/types"; |
|
|
| interface RawApiResponse { |
| rule_based_view_type: string[]; |
| review: boolean; |
| final_scores: number[]; |
| error: string | null; |
| file_details: { |
| main_img_name: string; |
| part_detection: string; |
| car_detection: string; |
| }; |
| } |
|
|
| export const classifyImage = async ( |
| file: File |
| ): Promise<ImageResult> => { |
| const formData = new FormData(); |
| formData.append("files", file); |
|
|
| const response = await fetch(`${BASE_URL}/api/classify-img`, { |
| method: "POST", |
| body: formData, |
| }); |
|
|
| if (!response.ok) { |
| throw new Error(`API error ${response.status}: ${response.statusText}`); |
| } |
|
|
| const results: RawApiResponse[] = await response.json(); |
| const first = results[0]; |
|
|
| const rawError = first?.error; |
| const errorMsg = rawError |
| ? typeof rawError === "string" |
| ? rawError |
| : JSON.stringify(rawError) |
| : null; |
|
|
| if (!first || errorMsg) { |
| return { |
| filename: file.name, |
| angles: [], |
| scores: [], |
| review: false, |
| error: errorMsg ?? "Classification failed", |
| fileDetails: null, |
| }; |
| } |
|
|
| return { |
| filename: file.name, |
| angles: (first.rule_based_view_type ?? []) as AngleLabel[], |
| scores: first.final_scores ?? [], |
| review: first.review ?? false, |
| error: null, |
| fileDetails: first.file_details ?? null, |
| }; |
| }; |
|
|
| export const getImageUrl = (path: string) => |
| `${BASE_URL}/files/${path}`; |
|
|