微信扫码
与创始人交个朋友
我要投稿
使用 Raspberry Pi AI 套件进行非结构化数据处理——Hailo边缘AI
非结构化数据处理、Raspberry Pi 5、Raspberry Pi AI套件、Milvus、Zilliz、数据、图像、计算机视觉、深度学习、Python
在边缘实时相机流中检测、显示和存储检测到的图像
!pip install boto3
Requirement already satisfied: boto3 in ./milvusvenv/lib/python3.12/site-packages (1.34.129) Requirement already satisfied: botocore<1.35.0,>=1.34.129 in ./milvusvenv/lib/python3.12/site-packages (from boto3) (1.34.129) Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in ./milvusvenv/lib/python3.12/site-packages (from boto3) (1.0.1) Requirement already satisfied: s3transfer<0.11.0,>=0.10.0 in ./milvusvenv/lib/python3.12/site-packages (from boto3) (0.10.1) Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in ./milvusvenv/lib/python3.12/site-packages (from botocore<1.35.0,>=1.34.129->boto3) (2.9.0.post0) Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in ./milvusvenv/lib/python3.12/site-packages (from botocore<1.35.0,>=1.34.129->boto3) (2.2.1) Requirement already satisfied: six>=1.5 in ./milvusvenv/lib/python3.12/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.129->boto3) (1.16.0)
from __future__ import print_functionimport requestsimport sysimport ioimport jsonimport shutilimport sysimport datetimeimport subprocessimport sysimport osimport mathimport base64from time import gmtime, strftimeimport random, stringimport timeimport psutilimport base64import uuidimport socketimport osfrom pymilvus import connectionsfrom pymilvus import utilityfrom pymilvus import FieldSchema, CollectionSchema, DataType, Collectionimport torchfrom torchvision import transformsfrom PIL import Imageimport timmfrom sklearn.preprocessing import normalizefrom timm.data import resolve_data_configfrom timm.data.transforms_factory import create_transformfrom pymilvus import MilvusClientimport osfrom IPython.display import display
from __future__ import print_functionimport requestsimport sysimport ioimport jsonimport shutilimport sysimport datetimeimport subprocessimport sysimport osimport mathimport base64from time import gmtime, strftimeimport random, stringimport timeimport psutilimport base64import uuidimport socketimport osfrom pymilvus import connectionsfrom pymilvus import utilityfrom pymilvus import FieldSchema, CollectionSchema, DataType, Collectionimport torchfrom torchvision import transformsfrom PIL import Imageimport timmfrom sklearn.preprocessing import normalizefrom timm.data import resolve_data_configfrom timm.data.transforms_factory import create_transformfrom pymilvus import MilvusClientimport osfrom IPython.display import display
# -----------------------------------------------------------------------------
class FeatureExtractor:
def __init__(self, modelname):
# Load the pre-trained model
self.model = timm.create_model(
modelname, pretrained=True, num_classes=0, global_pool="avg"
)
self.model.eval()
# Get the input size required by the model
self.input_size = self.model.default_cfg["input_size"]
config = resolve_data_config({}, model=modelname)
# Get the preprocessing function provided by TIMM for the model
self.preprocess = create_transform(**config)
def __call__(self, imagepath):
# Preprocess the input image
input_image = Image.open(imagepath).convert("RGB")# Convert to RGB if needed
input_image = self.preprocess(input_image)
# Convert the image to a PyTorch tensor and add a batch dimension
input_tensor = input_image.unsqueeze(0)
# Perform inference
with torch.no_grad():
output = self.model(input_tensor)
# Extract the feature vector
feature_vector = output.squeeze().numpy()
return normalize(feature_vector.reshape(1, -1), norm="l2").flatten()
extractor = FeatureExtractor("resnet34")
# -----------------------------------------------------------------------------
# Constants - should be environment variables
# -----------------------------------------------------------------------------
DIMENSION = 512
MILVUS_URL = "http://192.168.1.163:19530"
COLLECTION_NAME = "pidetections"
BUCKET_NAME = "images"
DOWNLOAD_DIR = "/Users/timothyspann/Downloads/code/images/"
AWS_RESOURCE = "s3"
S3_ENDPOINT_URL = "http://192.168.1.163:9000"
AWS_ACCESS_KEY = "minioadmin"
AWS_SECRET_ACCESS_KEY = "minioadmin"
S3_SIGNATURE_VERSION = "s3v4"
AWS_REGION_NAME = "us-east-1"
S3_ERROR_MESSAGE = "Download failed"
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Connect to Milvus
# Local Docker Server
milvus_client = MilvusClient( uri=MILVUS_URL)
# -----------------------------------------------------------------------------
import os
import boto3
from botocore.client import Config
# -----------------------------------------------------------------------------
# Access Images on S3 Compatible Store - AWS S3 or Minio or ...
# -----------------------------------------------------------------------------
s3 = boto3.resource(AWS_RESOURCE,
endpoint_url=S3_ENDPOINT_URL,
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
config=Config(signature_version=S3_SIGNATURE_VERSION),
region_name=AWS_REGION_NAME)
bucket = s3.Bucket(BUCKET_NAME)
# -----------------------------------------------------------------------------
# Get last modified image
# -----------------------------------------------------------------------------
files = bucket.objects.filter()
files = [obj.key for obj in sorted(files, key=lambda x: x.last_modified,
reverse=True)]
for imagename in files:
query_image = imagename
break
search_image_name = DOWNLOAD_DIR + query_image
try:
s3.Bucket(BUCKET_NAME).download_file(query_image, search_image_name)
except botocore.exceptions.ClientError as e:
print(S3_ERROR_MESSAGE)
# -----------------------------------------------------------------------------
# Search Milvus for that vector and filter by a label
# -----------------------------------------------------------------------------
results = milvus_client.search(
COLLECTION_NAME,
data=[extractor(search_image_name)],
filter='label in ["keyboard"]',
output_fields=["label", "confidence", "id", "s3path", "filename"],
search_params={"metric_type": "COSINE"},
limit=5
)
# -----------------------------------------------------------------------------
# Iterate through last five results and display metadata and image
# -----------------------------------------------------------------------------
for result in results:
for hit in result[:5]:
label = hit["entity"]["label"]
confidence = hit["entity"]["confidence"]
filename = hit["entity"]["filename"]
s3path = hit["entity"]["s3path"]
try:
s3.Bucket(BUCKET_NAME).download_file(filename, DOWNLOAD_DIR + filename)
except botocore.exceptions.ClientError as e:
print(S3_ERROR_MESSAGE)
print(f"Detection: {label} {confidence:.2f} for {filename} from {s3path}" )
img = Image.open(DOWNLOAD_DIR + filename)
display(img)
# Enhancement:we could also post this to slack or discord
tspann@five:/opt/demo $
hailortcli fw-control identify
Executing on device: 0000:01:00.0
Identifying board
Control Protocol Version: 2
Firmware Version: 4.17.0 (release,app,extended context switch buffer)
Logger Version: 0
Board Name: Hailo-8
Device Architecture: HAILO8L
Serial Number: HLDDLBB241601635
Part Number: HM21LB1C2LAE
Product Name: HAILO-8L AI ACC M.2 B+M KEY MODULE EXT TMP
tspann@five:/opt/demo $
dmesg | grep -i hailo
[3.155152] hailo: Init module. driver version 4.17.0
[3.155295] hailo 0000:01:00.0: Probing on: 1e60:2864...
[3.155301] hailo 0000:01:00.0: Probing: Allocate memory for device extension, 11600
[3.155321] hailo 0000:01:00.0: enabling device (0000 -> 0002)
[3.155327] hailo 0000:01:00.0: Probing: Device enabled
[3.155350] hailo 0000:01:00.0: Probing: mapped bar 0 - 0000000095e362ea 16384
[3.155357] hailo 0000:01:00.0: Probing: mapped bar 2 - 000000005e2b2b7e 4096
[3.155362] hailo 0000:01:00.0: Probing: mapped bar 4 - 000000008db50d03 16384
[3.155365] hailo 0000:01:00.0: Probing: Force setting max_desc_page_size to 4096 (recommended value is 16384)
[3.155375] hailo 0000:01:00.0: Probing: Enabled 64 bit dma
[3.155378] hailo 0000:01:00.0: Probing: Using userspace allocated vdma buffers
[3.155382] hailo 0000:01:00.0: Disabling ASPM L0s
[3.155385] hailo 0000:01:00.0: Successfully disabled ASPM L0s
[3.417111] hailo 0000:01:00.0: Firmware was loaded successfully
[3.427885] hailo 0000:01:00.0: Probing: Added board 1e60-2864, /dev/hailo0
gst-inspect-1.0 hailotoolslspci | grep Hailouname -av4l2-ctl --list-formats-ext -d /dev/video0ls /dev/video*ffplay -f v4l2 /dev/video0
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2024-12-23
CPU、GPU 和 TPU 之间有什么区别
2024-12-21
台前调度是未来XR、AI工作流的重要交互方式
2024-12-21
NVIDIA全栈AI战略:从GPU到AI工作流的演进
2024-12-21
深度|AI 的下个十年,藏不住了!
2024-12-20
突破科技界限:OPPO 与 Azure 携手塑造智能手机新体验|智有可为
2024-12-20
Nvidia 的 CUDA 护城河到底有多深?
2024-12-20
9.3K Star 全能电脑AI助手!ScreenPipe:离线版 Rewind.ai,智能记录你的电脑活动
2024-12-20
火山引擎与FoloToy,乐鑫等企业联合发布 AI + 硬件智跃计划
2024-03-30
2024-05-09
2024-07-07
2024-07-23
2024-07-01
2024-06-24
2024-06-08
2024-06-05
2024-06-21
2024-07-11
2024-12-20
2024-12-15
2024-11-12
2024-11-11
2024-10-29
2024-10-22
2024-10-18
2024-10-16