调试图片上传和识别

This commit is contained in:
lewis 2025-04-14 09:40:54 +08:00
parent f9d969c85c
commit e6e0eaadab
2 changed files with 57 additions and 0 deletions

View File

@ -11,6 +11,12 @@ use app\admin\validate\store_product\StoreProductValidate;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Tiia\V20190529\Models\CreateImageRequest;
use TencentCloud\Tiia\V20190529\Models\SearchImageRequest;
use TencentCloud\Tiia\V20190529\TiiaClient;
use Webman\RedisQueue\Redis;
/**
@ -21,6 +27,7 @@ use Webman\RedisQueue\Redis;
class StoreProductController extends BaseAdminController
{
public $notNeedLogin = ['upload', 'recognition'];
/**
* @notes 获取商品列表列表
@ -151,4 +158,49 @@ class StoreProductController extends BaseAdminController
return $this->success('删除成功', [], 1, 1);
}
public function upload()
{
$file = $this->request->file('file');
$cred = new Credential(getenv('TENCENT_SECRET_ID'), getenv('TENCENT_SECRET_KEY'));
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("tiia.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new TiiaClient($cred, "ap-guangzhou", $clientProfile);
$req = new CreateImageRequest();
$file = file_get_contents(public_path() . '/可口可乐.jpg');
$params = [
'GroupId' => 'default',
'EntityId' => '可口可乐_330ml_听装',
'PicName' => '可口可乐.jpg',
'ImageBase64' => base64_encode($file)
];
$req->fromJsonString(json_encode($params));
$resp = $client->CreateImage($req);
$result = json_decode($resp->toJsonString(), true);
return $this->data($result);
}
public function recognition()
{
$cred = new Credential(getenv('TENCENT_SECRET_ID'), getenv('TENCENT_SECRET_KEY'));
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("tiia.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new TiiaClient($cred, "ap-guangzhou", $clientProfile);
$req = new SearchImageRequest();
$file = file_get_contents(public_path() . '/可口可乐.jpg');
// $file = file_get_contents(public_path() . '/可口可乐2.jpg');
// $file = file_get_contents(public_path() . '/百事可乐.jpg');
$params = [
'GroupId' => 'default',
'ImageBase64' => base64_encode($file)
];
$req->fromJsonString(json_encode($params));
$resp = $client->SearchImage($req);
$result = json_decode($resp->toJsonString(), true);
return $this->data($result);
}
}

View File

@ -24,4 +24,9 @@ class StoreProductPrice extends BaseModel
// 不生成该表的日志
public $doNotRecordLog = true;
public function product()
{
return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field('id,store_name,top_cate_id,two_cate_id,cate_id');
}
}