File size: 1,021 Bytes
92633fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { utapi } from "../../../../utils/uploadThing";
import { pipeline } from '@xenova/transformers';
export async function POST(req: Request, res: Response) {
    //get the uplaod thing url
    const formData = await req.formData();
    const files = formData.getAll("files");
    const response = await utapi.uploadFiles(files);
    const responseData = response[0].data;
    const url = responseData?.url;
    console.log(url);

    // detect it using onnx model
    const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
    const output = await detector(url);
    console.log(output);
    
    const countObj:{[key:string]:number} = {};
    output.forEach(({score, label}: any) => {
        if(score>0.5){
            if(countObj[label]){
                countObj[label] += 1;
            }else{
                countObj[label] = 1;
            }
        }
    });
    return new Response(JSON.stringify({
        url:url,
        label: JSON.stringify(countObj),
    }), { status: 200 });
}