https://medium.com/@lmeyer./get-an-error-free-e-commerce-web-site-using-sentry-b6061264efc8...
Transformer une image en texte grâce à OCR sur Prestashop 1.7
$ sudo apt install tesseract-ocr
https://pyimagesearch.com/2021/08/16/installing-tesseract-pytesseract-and-python-ocr-packages-on-your-system/
https://www.geeksforgeeks.org/how-to-extract-text-from-images-with-python/
pip install pytesseract
installation de composer
https://github.com/thiagoalessio/tesseract-ocr-for-php
module prestashop
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
use Symfony\Component\Filesystem\Exception\IOException;
use thiagoalessio\TesseractOCR\TesseractOCR;
class baba_tesseractocr extends Module
{
public function __construct()
{
$this->name = 'baba_tesseractocr';
$this->tab = 'others';
$this->version = '1.0.0';
$this->author = 'presta.cafe';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('baba_tesseractocr');
$this->description = $this->l('tesseract ocr');
}
/**
* Installation du module
* @return bool
*/
public function install()
{
if (!parent::install()
) {
return false;
}
return true;
}
/**
* Load the configuration form
*/
public function getContent()
{
/**
* If values have been submitted in the form, process.
*/
if (((bool)Tools::isSubmit('submitBabaOcr')) == true) {
$this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
#return $output.$this->renderForm();
return $this->renderForm();
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitBabaOcr';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => [
['name'=>'image','label' => 'Image','type' => 'file',],
],
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
'BABAREDIRECT301_LIVE_MODE' => Configuration::get('BABAREDIRECT301_LIVE_MODE', true),
'BABAREDIRECT301_ACCOUNT_EMAIL' => Configuration::get('BABAREDIRECT301_ACCOUNT_EMAIL', 'contact@prestashop.com'),
'BABAREDIRECT301_ACCOUNT_PASSWORD' => Configuration::get('BABAREDIRECT301_ACCOUNT_PASSWORD', null),
);
}
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
foreach (array_keys($form_values) as $key) {
Configuration::updateValue($key, Tools::getValue($key));
}
$newName = $_FILES['image']['name'];
$uploader = new \Uploader('image');
$uploader->setSavePath(_PS_MODULE_DIR_.'/baba_tesseractocr/images');
$files = $uploader->process($newName);
$tesseract = new TesseractOCR($uploader->files[0]['save_path']);
$text = $tesseract->run();
echo "<pre>";
var_dump($uploader->files[0]['save_path']);
echo "</pre>";
echo $text;
die();
}
/**
* Désinstallation du module
* @return bool
*/
public function uninstall()
{
if (
!parent::uninstall()
) {
return false;
}
return true;
}
}
Publié dans:
SEO Prestashop
Laissez un commentaire