我的商品库增加AI识图

This commit is contained in:
chfei committed 2026-11-06 19:23:00 +08:00
1 parent ff94184a0c
commit 013d343fe9
12 files changed
+880 -377

No files matched your search

+53 -15
View File
@@ -77,16 +77,46 @@ public static class GoodsComplianceService
SavePending(path, pending);
var batch = new ComplianceBatchResult { Unfinished = selected.Count };
foreach (var item in selected)
foreach (ComplianceGoods item in selected)
{
if (cancellationToken.IsCancellationRequested) break;
update(item.id, "读取详情中");
ComplianceResult result;
try
{
//item.type
var product = await Task.Run(() => readProduct(item), cancellationToken);
result = await DetectProductAsync(product, options, words,
number => update(item.id, $"正在检测第 {number} 张图片"), cancellationToken);
if (result.Error == null)
{
try
{
if (item.type == ComplianceGoods.Library)
{
var url = BaseClass.BuildPDDObjString("product/SaveProductSafety");
var data = UserLogin.GetSignature();
data += "&user_name=" + BaseClass.UserName;
data += "&id=" + item.id;
string html = await GetHtml.PostJsonAsync(url, data);
}
else
{
await StoreApiClient.Instance.CloudGoods.SaveProductSafety(new SaveProductSafetyRequest
{
UserName = GetUserName(),
ProductId = product.ProductId,
IsSafe = result.Hits.Length == 0
});
}
}
catch (Exception ex)
{
result.Error = $"检测结果为{result.Status},上报失败:{ex.Message}";
}
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
@@ -149,7 +179,24 @@ public static class GoodsComplianceService
foreach (var product in products)
{
started?.Invoke(product);
completed(product, await DetectProductAsync(product, options, words));
var result = await DetectProductAsync(product, options, words);
if (result.Error == null)
{
try
{
await StoreApiClient.Instance.CloudGoods.SaveProductSafety(new SaveProductSafetyRequest
{
UserName = GetUserName(),
ProductId = product.ProductId,
IsSafe = result.Hits.Length == 0
});
}
catch (Exception ex)
{
result.Error = $"检测结果为{result.Status},上报失败:{ex.Message}";
}
}
completed(product, result);
}
}
@@ -185,6 +232,9 @@ public static class GoodsComplianceService
var jpeg = await DownloadInspectionImageAsync(url);
cancellationToken.ThrowIfCancellationRequested();
var lines = await StoreApiClient.Instance.CloudGoods.RecognizeAsync(BaseClass.GetMd5(url), jpeg);
BaseClass.ImageAnalysisQuota -= 1;
var imageText = string.Join(Environment.NewLine, lines);
texts.AddRange(lines);
result.Hits = Matches(imageText, words);
@@ -202,19 +252,7 @@ public static class GoodsComplianceService
cancellationToken.ThrowIfCancellationRequested();
if (imageNumber == 0) throw new InvalidDataException("所选检测范围内没有图片。");
result.Text = string.Join(Environment.NewLine, texts);
try
{
await StoreApiClient.Instance.CloudGoods.SaveProductSafety(new SaveProductSafetyRequest
{
UserName = GetUserName(),
ProductId = product.ProductId,
IsSafe = result.Hits.Length == 0
});
}
catch (Exception ex)
{
result.Error = $"检测结果为{result.Status},上报失败:{ex.Message}";
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { throw; }
catch (Exception ex) { result.Error = ex.Message; }
+2
View File
@@ -101,6 +101,8 @@ namespace Work
BaseClass.OemId = BaseRegex.GetStringJson(ref html, "oem_id");
BaseClass.OemName = BaseRegex.GetStringJson(ref html, "oem_name");
BaseClass.IsOemOwner = BaseRegex.GetBoolJson(ref html, "is_oem_owner");
BaseClass.ImageAnalysisQuota = Math.Max(0, BaseRegex.GetIntJson(ref html, "image_analysis_quota"));
#if DEBUG
if (!string.IsNullOrEmpty(BaseRegex.GetStringJson(ref html, "prod_api_url")) && !BaseClass.LoginApiUrl.Contains("localhost") && !BaseClass.LoginApiUrl.Contains("127.0.0.1"))
{