Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ai company
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
ai company
Commits
059a13d4
Commit
059a13d4
authored
May 12, 2026
by
Vũ Hoàng Anh
Browse files
Options
Browse Files
Download
Plain Diff
Merge feature/ocr-vision into main
parents
bbb548b6
451352c1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
132 additions
and
26 deletions
+132
-26
image_search_graph.py
backend/agent/image_search_agent/image_search_graph.py
+32
-1
vision_model.py
backend/agent/image_search_agent/vision_model.py
+56
-25
test_ocr_vision.py
backend/test_ocr_vision.py
+41
-0
uv.lock
backend/uv.lock
+3
-0
No files found.
backend/agent/image_search_agent/image_search_graph.py
View file @
059a13d4
...
@@ -137,8 +137,39 @@ class ImageSearchGraph:
...
@@ -137,8 +137,39 @@ class ImageSearchGraph:
if
analysis
.
get
(
"success"
):
if
analysis
.
get
(
"success"
):
feats
=
analysis
[
"features"
]
feats
=
analysis
[
"features"
]
extracted_features_text
+=
f
"Ảnh {idx+1} [Local Vision AI]: Danh mục: {feats.get('category')}, Màu: {feats.get('color')}, Phong cách: {feats.get('style')}
\n
"
ocr_text
=
feats
.
get
(
"ocr_text"
,
""
)
ocr_info
=
f
", Chữ in trên áo (OCR): {ocr_text}"
if
ocr_text
else
""
extracted_features_text
+=
f
"Ảnh {idx+1} [Local Vision AI + OCR]: Danh mục: {feats.get('category')}, Màu: {feats.get('color')}, Phong cách: {feats.get('style')}{ocr_info}
\n
"
extracted_features_text
+=
f
"Raw tags: {', '.join(feats.get('raw_labels', []))}
\n
"
extracted_features_text
+=
f
"Raw tags: {', '.join(feats.get('raw_labels', []))}
\n
"
if
ocr_text
:
import
sqlite3
from
common.constants
import
SQLITE_DB_PATH
try
:
conn
=
sqlite3
.
connect
(
SQLITE_DB_PATH
)
conn
.
row_factory
=
sqlite3
.
Row
cursor
=
conn
.
cursor
()
keywords
=
[
k
for
k
in
ocr_text
.
split
()
if
len
(
k
)
>
2
]
if
keywords
:
like_clauses
=
" OR "
.
join
([
"clean_description LIKE ? OR product_name LIKE ?"
]
*
len
(
keywords
))
params
=
[]
for
k
in
keywords
:
params
.
extend
([
f
"
%
{k}
%
"
,
f
"
%
{k}
%
"
])
rows
=
cursor
.
execute
(
f
"SELECT product_name, internal_ref_code, product_image_url "
f
"FROM pg__dashboard_canifa__ultra_descriptions "
f
"WHERE {like_clauses} LIMIT 5"
,
params
)
.
fetchall
()
if
rows
:
extracted_features_text
+=
f
"→ Đã tìm thấy {len(rows)} sản phẩm khớp chữ '{ocr_text}' trong SQLite:
\n
"
for
r
in
rows
:
extracted_features_text
+=
f
" - [{r['internal_ref_code']}] {r['product_name']}
\n
"
conn
.
close
()
except
Exception
as
e
:
logger
.
error
(
f
"Lỗi truy vấn SQLite OCR: {e}"
)
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
f
"Lỗi đọc ảnh cho Local Vision Model: {e}"
)
logger
.
error
(
f
"Lỗi đọc ảnh cho Local Vision Model: {e}"
)
...
...
backend/agent/image_search_agent/vision_model.py
View file @
059a13d4
...
@@ -128,6 +128,16 @@ class LocalVisionModel:
...
@@ -128,6 +128,16 @@ class LocalVisionModel:
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
"Lỗi khởi tạo Vision Model:
%
s"
,
e
)
logger
.
error
(
"Lỗi khởi tạo Vision Model:
%
s"
,
e
)
self
.
_classifier
=
None
self
.
_classifier
=
None
# Khởi tạo RapidOCR siêu nhẹ
try
:
from
rapidocr_onnxruntime
import
RapidOCR
logger
.
info
(
"Khởi tạo RapidOCR (ONNX) siêu nhẹ cho nhận diện chữ..."
)
self
.
_ocr
=
RapidOCR
()
logger
.
info
(
"Đã khởi tạo thành công RapidOCR."
)
except
ImportError
:
logger
.
warning
(
"Chưa cài đặt RapidOCR. Chạy: uv pip install rapidocr-onnxruntime"
)
self
.
_ocr
=
None
def
analyze_image
(
self
,
image_path
:
str
)
->
dict
:
def
analyze_image
(
self
,
image_path
:
str
)
->
dict
:
"""
"""
...
@@ -135,35 +145,55 @@ class LocalVisionModel:
...
@@ -135,35 +145,55 @@ class LocalVisionModel:
Returns:
Returns:
dict with keys: success, features, confidence
dict with keys: success, features, confidence
features contains: raw_labels, category, color, style, all_categories
features contains: raw_labels, category, color, style, all_categories
, ocr_text
"""
"""
if
not
self
.
_classifier
:
return
{
"error"
:
"Vision model chưa sẵn sàng. Vui lòng kiểm tra logs."
}
try
:
try
:
# 1. Load image
# 1. Load image
img
=
Image
.
open
(
image_path
)
.
convert
(
"RGB"
)
img
=
Image
.
open
(
image_path
)
.
convert
(
"RGB"
)
# 2. Classify via model
category
=
"unknown"
predictions
=
self
.
_classifier
(
img
)
color
=
"unknown"
style
=
"casual"
# 3. Filter by confidence threshold
all_categories
=
[]
strong_predictions
=
[
top_tags
=
[]
p
for
p
in
predictions
if
p
[
"score"
]
>=
MIN_CONFIDENCE
confidence
=
1.0
]
if
not
strong_predictions
:
# 2. Classify via model (if available)
strong_predictions
=
predictions
[:
1
]
# Keep at least top-1
if
self
.
_classifier
:
try
:
top_tags
=
[
p
[
"label"
]
for
p
in
strong_predictions
[:
5
]]
predictions
=
self
.
_classifier
(
img
)
tags_str
=
" "
.
join
(
top_tags
)
.
lower
()
strong_predictions
=
[
p
for
p
in
predictions
if
p
[
"score"
]
>=
MIN_CONFIDENCE
]
if
not
strong_predictions
:
# 4. Extract features using curated rules
strong_predictions
=
predictions
[:
1
]
category
=
self
.
_match_first
(
tags_str
,
CATEGORY_RULES
,
"unknown"
)
color
=
self
.
_match_first
(
tags_str
,
COLOR_RULES
,
"unknown"
)
top_tags
=
[
p
[
"label"
]
for
p
in
strong_predictions
[:
5
]]
style
=
self
.
_match_first
(
tags_str
,
STYLE_RULES
,
"casual"
)
tags_str
=
" "
.
join
(
top_tags
)
.
lower
()
# Collect ALL matching categories (for multi-tag search)
category
=
self
.
_match_first
(
tags_str
,
CATEGORY_RULES
,
"unknown"
)
all_categories
=
self
.
_match_all
(
tags_str
,
CATEGORY_RULES
)
color
=
self
.
_match_first
(
tags_str
,
COLOR_RULES
,
"unknown"
)
style
=
self
.
_match_first
(
tags_str
,
STYLE_RULES
,
"casual"
)
all_categories
=
self
.
_match_all
(
tags_str
,
CATEGORY_RULES
)
confidence
=
strong_predictions
[
0
][
"score"
]
except
Exception
as
e
:
logger
.
error
(
"Lỗi khi phân loại ảnh:
%
s"
,
e
)
# 5. Extract Text via RapidOCR (ONNX CPU)
extracted_text
=
[]
if
self
.
_ocr
:
try
:
import
numpy
as
np
# Convert PIL RGB to OpenCV BGR format
img_cv
=
np
.
array
(
img
)
img_cv
=
img_cv
[:,
:,
::
-
1
]
.
copy
()
ocr_result
,
_
=
self
.
_ocr
(
img_cv
)
if
ocr_result
:
# ocr_result format: [[box, text, confidence], ...]
for
box
,
text
,
score
in
ocr_result
:
if
score
>
0.3
:
# Độ tin cậy > 30% để lấy chữ in
extracted_text
.
append
(
text
)
except
Exception
as
e
:
logger
.
error
(
"Lỗi khi chạy RapidOCR:
%
s"
,
e
)
extracted_features
=
{
extracted_features
=
{
"raw_labels"
:
top_tags
,
"raw_labels"
:
top_tags
,
...
@@ -171,12 +201,13 @@ class LocalVisionModel:
...
@@ -171,12 +201,13 @@ class LocalVisionModel:
"color"
:
color
,
"color"
:
color
,
"style"
:
style
,
"style"
:
style
,
"all_categories"
:
all_categories
,
"all_categories"
:
all_categories
,
"ocr_text"
:
" "
.
join
(
extracted_text
)
}
}
return
{
return
{
"success"
:
True
,
"success"
:
True
,
"features"
:
extracted_features
,
"features"
:
extracted_features
,
"confidence"
:
strong_predictions
[
0
][
"score"
]
,
"confidence"
:
confidence
,
}
}
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
"Lỗi khi phân tích ảnh:
%
s"
,
e
)
logger
.
error
(
"Lỗi khi phân tích ảnh:
%
s"
,
e
)
...
...
backend/test_ocr_vision.py
0 → 100644
View file @
059a13d4
import
asyncio
from
PIL
import
Image
,
ImageDraw
import
base64
import
io
def
create_dummy_image_with_text
():
# Create a blank black image
img
=
Image
.
new
(
'RGB'
,
(
300
,
300
),
color
=
(
0
,
0
,
0
))
d
=
ImageDraw
.
Draw
(
img
)
# Add text "CANIFA" to the image
d
.
text
((
50
,
150
),
"CANIFA"
,
fill
=
(
255
,
255
,
255
))
# Save to base64
buffered
=
io
.
BytesIO
()
img
.
save
(
buffered
,
format
=
"JPEG"
)
img_str
=
base64
.
b64encode
(
buffered
.
getvalue
())
.
decode
()
return
img_str
async
def
test
():
print
(
"Testing Image Vision OCR with SQLite..."
)
img_b64
=
create_dummy_image_with_text
()
from
agent.image_search_agent.image_search_graph
import
get_image_search_agent
agent
=
get_image_search_agent
()
print
(
"Running Image Search Agent..."
)
result
=
await
agent
.
chat
(
user_message
=
"Tìm cho tôi chiếc áo này"
,
images
=
[
img_b64
])
print
(
"
\n
=== AI RESPONSE ==="
)
print
(
result
.
get
(
"response"
))
print
(
"===================
\n
"
)
print
(
"--- Pipeline Trace ---"
)
for
diag
in
result
.
get
(
"pipeline"
,
[]):
print
(
f
"Step: {diag.get('label')}"
)
if
diag
.
get
(
"step"
)
==
"user"
:
print
(
diag
.
get
(
"content"
))
if
__name__
==
"__main__"
:
asyncio
.
run
(
test
())
\ No newline at end of file
backend/uv.lock
0 → 100644
View file @
059a13d4
version = 1
revision = 3
requires-python = ">=3.14"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment