Piwigo / piwigo-tag-recognition

Piwigo plugin to suggest tags on image (using external API)
3 stars 6 forks source link

API extension for CompreFace #7

Open 3JL opened 1 year ago

3JL commented 1 year ago

Great plugin!! :) I extended it to also work with CompreFace which is a facial recognition algorithm that can run locally. Below the code. It will need to be refined a bit here and there (I am far from a professional) and I am more than happy to help doing so. I can also write a tutorial or something if needed. Best, J

`<?php

class CompreFace extends API {

function getInfo() : array
{
    return [
        "icon" => 'https://user-images.githubusercontent.com/3736126/147130206-17234c47-8d40-490f-8d93-57014fa6d87e.png',
        "site" => 'https://exadel.com/solutions/compreface/',
        "info" => `
        CompreFace is an open-source face recognition service that can be run on premises
        `,
    ];
}

function getConfParams() : array
{
    return [
        'ENDPOINT' => 'API Endpoint', 
        'KEY'=> 'API Key'
    ];
}

function generateTags($conf, $params) : array
{
    global $logger;
    $file_path = $this->getFileName($params['imageId']);

    $url ='http://'.$conf["ENDPOINT"].'/api/v1/recognition/recognize?limit=0&det_prob_threshold=0.6&prediction_count=1&face_plugins=landmarks,%20gender,%20age,%20calculator&status=true';

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => array('file'=> new CURLFILE($file_path)),
        CURLOPT_HTTPHEADER => array(
            'Content-Type: multipart/form-data',
            'x-api-key: '.$conf["KEY"]
        ),
    ));

    if (curl_errno($curl)) 
    {
        return [curl_error($curl)];
    }

    $response = curl_exec($curl);

    curl_close($curl);

    $json_response = json_decode($response);

    $tags = [];

    $json_result = $json_response->result;

    foreach ($json_result as $result) {

        if($result->subjects[0]->similarity > 0.65)
            array_push($tags,$result->subjects[0]->subject);
        $logger->info("Found " . $result->subjects[0]->subject);
    }

    return $tags;
}

}`

3JL commented 1 year ago

Some specific issues that I ran into using CompreFace that need to be resolved. I would benefit from a bit of help here :) @Zacharieg, would you be able to help out?

Issue 1 - error message despite successful tagging Screenshot 2023-03-28 at 11 32 47

Detailed description: when running the plugin from the Batch Manager (for a single or multiple photos), an error is returned, while the images are correctly processed and tags correctly added to the figure.

Issue 2 - success message despite plugin not running for second selection Screenshot 2023-03-28 at 11 37 16

Detailed description: when running the plugin from the Batch Manager (after a successful run) on a new selection, a success message is provided, but no photos are tagged.

Issue 3 - plugin hanging on 'Loading...' Screenshot 2023-03-28 at 11 40 05

Detailed description: when running the plugin from a photo page ('Edit photo'), the plugin hangs on 'Loading..' and no tagging takes place.

RStoroy commented 1 year ago

I have the same issues on ALL APIS.

kkygeek commented 1 year ago

Same here, either with Azure or Imagga. Did the cURL check with Imagga, which was succesful, so the problem seems to be with the plugin.