Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
image_search
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
service
image_search
Commits
1e69ea27
Commit
1e69ea27
authored
May 24, 2025
by
zhengyaoqiu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
上传 & 检索
parent
285b001c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
63 additions
and
9 deletions
+63
-9
routes.py
app/api/routes.py
+16
-1
feature_extractor.py
app/services/feature_extractor.py
+0
-0
image_search.py
app/services/image_search.py
+8
-4
image_upload.py
app/services/image_upload.py
+33
-0
milvus.py
app/services/milvus.py
+5
-3
test_feature_extractor.py
tests/test_feature_extractor.py
+1
-1
No files found.
app/api/routes.py
View file @
1e69ea27
from
flask
import
jsonify
from
flask
import
jsonify
,
request
from
app.api
import
api_bp
from
app.api
import
api_bp
@
api_bp
.
route
(
'/hello'
,
methods
=
[
'GET'
])
@
api_bp
.
route
(
'/hello'
,
methods
=
[
'GET'
])
...
@@ -16,3 +16,18 @@ def hello_name(name):
...
@@ -16,3 +16,18 @@ def hello_name(name):
'message'
:
f
'Hello, {name}!'
,
'message'
:
f
'Hello, {name}!'
,
'status'
:
'success'
'status'
:
'success'
})
})
@
api_bp
.
route
(
'/upload'
,
methods
=
[
'PUT'
])
def
upload
(
name
):
# 获取 JSON 格式的请求体数据
data
=
request
.
get_json
()
# 访问具体字段
bucket
=
data
.
get
(
'bucket'
)
image
=
data
.
get
(
'image'
)
key
=
data
.
get
(
'key'
)
return
jsonify
({
'message'
:
f
'Hello, {name}!'
,
'status'
:
'success'
})
app/
model
s/feature_extractor.py
→
app/
service
s/feature_extractor.py
View file @
1e69ea27
File moved
app/
model
s/image_search.py
→
app/
service
s/image_search.py
View file @
1e69ea27
...
@@ -8,19 +8,23 @@ class ImageSearch:
...
@@ -8,19 +8,23 @@ class ImageSearch:
self
.
milvus
=
milvus
self
.
milvus
=
milvus
# id = product_id
# id = product_id
def
image_to_image_search
(
self
,
image
,
key_nam
e
,
top_k
=
100
):
def
image_to_image_search
(
self
,
bucket
,
imag
e
,
top_k
=
100
):
try
:
try
:
# 提取查询图像的特征
# 提取查询图像的特征
query_embedding
=
self
.
feature_extractor
.
extract_features
(
image
)
vector
=
self
.
feature_extractor
.
extract_features
(
image
)
results
=
self
.
milvus
.
search
(
query_embedding
,
limit
=
top_k
)
# anns_field = embedding
# output_fields=["image", "product_id"]
# search_params: Dict[str, Any] = {"metric_type": "IP", "params": {"ef": 100}}
results
=
self
.
milvus
.
search
(
bucket
,
vector
,
top_k
)
# 处理结果
# 处理结果
if
not
results
or
len
(
results
)
==
0
:
if
not
results
or
len
(
results
)
==
0
:
return
[]
return
[]
# 返回结果
# 返回结果
keys
=
[
hit
.
entity
.
get
(
key_name
)
for
hit
in
results
[
0
]]
keys
=
[
hit
.
entity
.
get
(
"key"
)
for
hit
in
results
[
0
]]
scores
=
[
hit
.
score
for
hit
in
results
[
0
]]
scores
=
[
hit
.
score
for
hit
in
results
[
0
]]
return
list
(
zip
(
keys
,
scores
))
return
list
(
zip
(
keys
,
scores
))
...
...
app/services/image_upload.py
0 → 100644
View file @
1e69ea27
import
logging
class
Upload
:
__logger
=
logging
.
getLogger
(
__name__
)
def
__init__
(
self
,
feature_extractor
,
milvus
):
self
.
feature_extractor
=
feature_extractor
self
.
milvus
=
milvus
def
upload_one
(
self
,
bucket
,
image
,
key
):
self
.
upload_many
(
bucket
,
{
image
:
key
})
def
upload_many
(
self
,
bucket
,
image2key
):
images
=
[]
keys
=
[]
vectors
=
[]
for
image
,
key
in
image2key
.
items
():
vector
=
self
.
feature_extractor
.
extract_from_url
(
image
)
images
.
append
(
image
)
keys
.
append
(
key
)
vectors
.
append
(
vector
)
entities
=
[
images
,
keys
,
vectors
]
self
.
milvus
.
insert
(
bucket
,
entities
)
app/
model
s/milvus.py
→
app/
service
s/milvus.py
View file @
1e69ea27
...
@@ -46,15 +46,17 @@ class MilvusClient:
...
@@ -46,15 +46,17 @@ class MilvusClient:
# output_fields=["image", "product_id"]
# output_fields=["image", "product_id"]
# search_params: Dict[str, Any] = {"metric_type": "IP", "params": {"ef": 100}}
# search_params: Dict[str, Any] = {"metric_type": "IP", "params": {"ef": 100}}
def
search
(
self
,
collection_name
,
vector
,
anns_field
,
search_params
,
output_fields
,
top_k
=
10
)
->
Any
:
def
search
(
self
,
collection_name
,
vector
,
top_k
=
10
)
->
Any
:
collection
=
self
.
get_collection
(
collection_name
)
collection
=
self
.
get_collection
(
collection_name
)
search_params
:
Dict
[
str
,
Any
]
=
{
"metric_type"
:
"IP"
,
"params"
:
{
"ef"
:
100
}}
results
=
collection
.
search
(
results
=
collection
.
search
(
data
=
[
vector
],
data
=
[
vector
],
anns_field
=
anns_field
,
anns_field
=
"vector"
,
param
=
search_params
,
param
=
search_params
,
limit
=
top_k
,
limit
=
top_k
,
output_fields
=
output_fields
output_fields
=
[
"image"
,
"key"
]
)
)
return
results
return
results
...
...
tests/test_feature_extractor.py
View file @
1e69ea27
import
unittest
import
unittest
from
app.
model
s.feature_extractor
import
FeatureExtractor
from
app.
service
s.feature_extractor
import
FeatureExtractor
class
TestFeatureExtractorFunction
(
unittest
.
TestCase
):
class
TestFeatureExtractorFunction
(
unittest
.
TestCase
):
...
...
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