File size: 1,090 Bytes
84f8376
f3f6f5d
4bce717
 
f3f6f5d
 
 
4bce717
 
 
 
 
 
 
 
 
f3f6f5d
 
 
4bce717
f3f6f5d
 
 
 
4bce717
f3f6f5d
 
 
 
4bce717
 
f3f6f5d
 
 
 
4bce717
 
 
 
 
 
 
f3f6f5d
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const API_URL = (import.meta.env.VITE_API_URL as string) || 'http://localhost:8000';

export type ModelType = 'cars' | 'spots';

export interface Detection {
  bbox: [number, number, number, number];
  score: number;
  class_id: number;
  class_name: string;
}

export interface Occupancy {
  empty_count: number;
  occupied_count: number;
  total_spots: number;
  occupancy_rate: number;
}

export interface DetectResponse {
  model: string;
  car_count: number;
  detections: Detection[];
  annotated_image: string;
  heatmap_image: string;
  occupancy?: Occupancy;
}

export async function detectCars(
  file: File,
  threshold: number = 0.5,
  model: ModelType = 'cars'
): Promise<DetectResponse> {
  const form = new FormData();
  form.append('file', file);

  const res = await fetch(
    `${API_URL}/detect?threshold=${threshold}&model=${model}`,
    {
      method: 'POST',
      body: form,
    }
  );

  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: 'Request failed' }));
    throw new Error(err.error ?? `HTTP ${res.status}`);
  }

  return res.json();
}