Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] ocr QR screen #196

Open
dnsBlah opened this issue Sep 23, 2020 · 1 comment
Open

[feature] ocr QR screen #196

dnsBlah opened this issue Sep 23, 2020 · 1 comment
Labels
enhancement New feature or request help wanted Worth doing but needs an owner

Comments

@dnsBlah
Copy link
Contributor

dnsBlah commented Sep 23, 2020

Pull playername from screenshot, making use of ocr.space api

File based image

$ocrSpaceAPI = 'myAPIcodehere';
$uploadFile = 'local path to file to be uploaded';

$ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, 'https://api.ocr.space/parse/image');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'apikey: ' . $ocrSpaceAPI
    ));

    $imgType = pathinfo($uploadFile, PATHINFO_EXTENSION);
    $imgData = file_get_contents($uploadFile);
    $base64data = 'data:image/' . strtolower($imgType) . ';base64,' . base64_encode($imgData);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'base64Image' => trim($base64data)
    ));
    // Execute the request
    $chResponse = curl_exec($ch);
    if($chResponse === false) {
        //Something went wrong
    } else {
        $apiResult = json_decode($chResponse,true);
        if(is_array($apiResult)) {
            if(array_key_exists('OCRExitCode',$apiResult)) {
                if(intval($apiResult['OCRExitCode']) === 1) {
                    $ocrResults = '';
                    foreach($apiResult['ParsedResults'] as $partResult) {
                        $ocrResults .= $partResult['ParsedText'];
                    }                    
                    $ocrLines = explode('
',$ocrResults);
                    $tTrainername = false;
                    foreach($ocrLines as $ocrLine) {
                        if($tTrainername === true) {
                            $tTrainername = trim($ocrLine);
                            break;
                        }
                        if(stristr($ocrLine, 'TRAINER NAME')) {
                            $tTrainername = true;
                        }
                    }
                    if($tTrainername !== false && $tTrainername !== true) {
////Trainer name found here////
                    } else {
                        echo 'Unable to parse trainername?!';
                    }
                } else {
                    //Something went wrong
                    /*
                     * $apiResult['ErrorMessage']
                     * $apiResult['ErrorDetails']
                     */
                }
            } else {
                //Something went wrong, unexpected response
            }
        } else {
            //Something went wrong response from api is not an json array
        }
    }

URL based image

$ocrSpaceAPI = 'myAPIcodehere';
$imageURL = 'full public path to image';

$ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, 'https://api.ocr.space/parse/image');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'apikey: ' . $ocrSpaceAPI
    ));

    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'url' => trim($imageURL)
    ));
    // Execute the request
    $chResponse = curl_exec($ch);
    if($chResponse === false) {
        //Something went wrong
    } else {
        $apiResult = json_decode($chResponse,true);
        if(is_array($apiResult)) {
            if(array_key_exists('OCRExitCode',$apiResult)) {
                if(intval($apiResult['OCRExitCode']) === 1) {
                    $ocrResults = '';
                    foreach($apiResult['ParsedResults'] as $partResult) {
                        $ocrResults .= $partResult['ParsedText'];
                    }                    
                    $ocrLines = explode('
',$ocrResults);
                    $tTrainername = false;
                    foreach($ocrLines as $ocrLine) {
                        if($tTrainername === true) {
                            $tTrainername = trim($ocrLine);
                            break;
                        }
                        if(stristr($ocrLine, 'TRAINER NAME')) {
                            $tTrainername = true;
                        }
                    }
                    if($tTrainername !== false && $tTrainername !== true) {
////Trainer name found here////
                    } else {
                        echo 'Unable to parse trainername?!';
                    }
                } else {
                    //Something went wrong
                    /*
                     * $apiResult['ErrorMessage']
                     * $apiResult['ErrorDetails']
                     */
                }
            } else {
                //Something went wrong, unexpected response
            }
        } else {
            //Something went wrong response from api is not an json array
        }
    }
@jinnatar
Copy link
Collaborator

This is a good start but needs to be reworked either by the original author or by someone else interested into a full feature:

  • We need more information in this issue explaining the motivation & use case of the feature. My assumption that this is to allow users to send a screenshot of their friend code QR which is then used to parse their trainer name & friend code?
  • Once the motivation & design are clear, this should be turned into a PR against the dev branch.
  • If we want to support both file and URL based images, the two should be combined into one codebase that can handle both. For example, take common functionality and abstract it into a function, and then just have two different interface functions that do the url / file specific bits and call the internal function.
  • The function should be standalone but use our existing curl wrappers.
  • Config values need to be added for the API key and enabling the feature.
  • Documentation updates are needed to explain the feature.

If multiple individuals want to start developing this I'm happy to accept a PR for a new feature branch forked off dev to pool resources to make it happen. Once it's feature complete, open a PR to merge it to dev from where it'll become part of the next release.

@jinnatar jinnatar added enhancement New feature or request help wanted Worth doing but needs an owner labels Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Worth doing but needs an owner
Projects
None yet
Development

No branches or pull requests

2 participants