def run(): # 系统初始化,参数要与创建技能时填写的检验值保持一致 hilens.init("pose") # 初始化模型 pose_model_path = hilens.get_model_dir() + "pose_template_model.om" pose_model = hilens.Model(pose_model_path) # 初始化USB摄像头与HDMI显示器 camera = hilens.VideoCapture() display_hdmi = hilens.Display(hilens.HDMI) while True: # 读取一帧图片(BGR格式) input_yuv = camera.read() # 图片预处理:转为BGR格式、裁剪/缩放为模型输入尺寸 input_bgr = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2BGR_NV21) img_preprocess = preprocess(input_bgr) # 模型推理 model_outputs = pose_model.infer([img_preprocess.flatten()]) # 从推理结果中解码出人体关键点并画在图像中 points = get_points(input_bgr, model_outputs) img_data = draw_limbs(input_bgr, points) # 输出处理后的图像到HDMI显示器,必须先转换成YUV NV21格式 output_nv21 = hilens.cvt_color(img_data, hilens.BGR2YUV_NV21) display_hdmi.show(output_nv21) hilens.terminate()
def run(): # 系统初始化,参数要与创建技能时填写的检验值保持一致 hilens.init("mask") # 初始化摄像头 camera = hilens.VideoCapture() display = hilens.Display(hilens.HDMI) # 初始化模型 mask_model_path = hilens.get_model_dir() + "convert-mask-detection.om" mask_model = hilens.Model(mask_model_path) while True: ##### 1. 设备接入 ##### input_yuv = camera.read() # 读取一帧图片(YUV NV21格式) ##### 2. 数据预处理 ##### img_rgb = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2RGB_NV21) # 转为RGB格式 img_preprocess, img_w, img_h = preprocess(img_rgb) # 缩放为模型输入尺寸 ##### 3. 模型推理 ##### output = mask_model.infer([img_preprocess.flatten()]) ##### 4. 结果输出 ##### bboxes = get_result(output, img_w, img_h) # 获取检测结果 img_rgb = draw_boxes(img_rgb, bboxes) # 在图像上画框 output_yuv = hilens.cvt_color(img_rgb, hilens.RGB2YUV_NV21) display.show(output_yuv) # 显示到屏幕上 hilens.terminate()
def camera_thread(camera_address, server_url): # 初始化IPC camera = hilens.VideoCapture(camera_address) # 初始化模型 helmet_model_path = hilens.get_model_dir() + "helmet_template_model.om" helmet_model = hilens.Model(helmet_model_path) while True: # 读取一帧图片(YUV NV21格式) input_yuv = camera.read() # 图片预处理:转为RGB格式、缩放为模型输入尺寸 img_rgb = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2RGB_NV21) img_preprocess, img_w, img_h = preprocess(img_rgb) # 模型推理,并进行后处理得到检测框 output = helmet_model.infer([img_preprocess.flatten()]) bboxes = get_result(output, img_w, img_h) # 从检测结果中判断是否有未佩戴安全帽的人 if no_helmet(bboxes): # 后处理得到检测框,并在RGB图中画框 img_rgb = draw_boxes(img_rgb, bboxes) # 以POST方式传输数据 try: post_msg(server_url, img_rgb, bboxes) except Exception as e: hilens.error("post data failed!") print("Reason : ", e) # 等待5秒后再检测,避免发送数据过多 time.sleep(15)
def __init__(self, video_path="", hilens_enable=True, socket_enable=True, display_enable=True, model_enable=True, record_enable=False): self.__hilens_enable = hilens_enable self.__socket_enable = socket_enable self.__display_enable = display_enable self.__model_enable = model_enable self.__record_enable = record_enable if self.__hilens_enable: import hilens video_path = "" if self.__socket_enable: self.__socket_3399 = SocketThreadGuard(socket_enable=socket_enable) if self.__display_enable: self.__display = hilens.Display(hilens.HDMI) else: self.__display = None model = None if self.__model_enable: # 初始化模型 # model_path = hilens.get_model_dir() + "model-extreme-1-1.om" if self.__hilens_enable: model_path = hilens.get_model_dir() + "speed_detect.om" else: model_path = "model/convert-1360.om" # model_path = hilens.get_model_dir() + "convert-1360.om" model = hilens.Model(model_path) print("create model") self.__cam_thread = CameraThreadGuard(video_path, RECORD=self.__record_enable, HILENS=self.__hilens_enable) self.__pp_thread = PictureProcessorThreadGuard( model, HILENS=self.__hilens_enable, socket_enable=self.__socket_enable, display=self.__display)
def main(): """ 利用SkillFramework进行人脸检测模型的推理 """ hilens.init("test") # 参数要与创建技能时填写的检验值保持一致 model_path = hilens.get_model_dir() + "face_detection_demo.om" # 模型路径 model = hilens.Model(model_path) display_hdmi = hilens.Display(hilens.HDMI) # 图像通过hdmi输出到屏幕 camera = hilens.VideoCapture() while True: # 1. 读取摄像头输入(yuv nv21) input_nv21 = camera.read() # 2. 转为bgr input_bgr = cv2.cvtColor(input_nv21, cv2.COLOR_YUV2BGR_NV21) src_image_height = input_bgr.shape[0] src_image_width = input_bgr.shape[1] # 3. 保留原图比例的resize为网络输入尺寸 im_scale1 = float(input_width) / float(src_image_width) im_scale2 = float(input_height) / float(src_image_height) im_scale = min(im_scale1, im_scale2) input_bgr_rescaled = cv2.resize(input_bgr, None, None, fx=im_scale, fy=im_scale) input_bgr_resized = np.zeros((input_height, input_width, 3), dtype = np.uint8) input_bgr_resized[0:input_bgr_rescaled.shape[0],0:input_bgr_rescaled.shape[1],:] = input_bgr_rescaled # 3. 推理 outputs = model.infer([input_bgr_resized.flatten()]) # 4. 后处理得到人脸bounding box,恢复到原图比例,画人脸框 detect_boxes = im_detect_nms(outputs[0]) if len(detect_boxes) > 0: for rect in detect_boxes: left = max(rect[0] / im_scale, 0) top = max(rect[1] / im_scale, 0) right = min(rect[2] / im_scale, src_image_width) bottom = min(rect[3] / im_scale, src_image_height) cv2.rectangle(input_bgr, (int(left), int(top)), (int(right), int(bottom)), 255, 2) # 5. 输出图像,必须是yuv nv21形式 output_nv21 = hilens.cvt_color(input_bgr, hilens.BGR2YUV_NV21) display_hdmi.show(output_nv21)
def init(): global model, camera1, camera2, upload_uri1, upload_uri2, display_hdmi hilens.init("hello") model_path = hilens.get_model_dir() + "./convert-4label.om" model = hilens.Model(model_path) display_hdmi = hilens.Display(hilens.HDMI) hilens.set_log_level(hilens.DEBUG) skill_cfg = hilens.get_skill_config() if skill_cfg is None or 'IPC_address1' not in skill_cfg or 'upload_uri1' not in skill_cfg: hilens.fatal( 'Missing IPC1 skill configs! skill_cfg: {}'.format(skill_cfg)) hilens.terminate() exit(1) try: camera1 = hilens.VideoCapture(skill_cfg['IPC_address1']) except Exception as e: hilens.fatal("Failed to create camera1 with {}, e: {}", skill_cfg['IPC_address1'], e) upload_uri1 = skill_cfg['upload_uri1'] if 'IPC_address2' not in skill_cfg or 'upload_uri2' not in skill_cfg: hilens.warning( 'Missing IPC2 skill configs! Camera2 will not work. skill_cfg: {}'. format(skill_cfg)) else: try: camera2 = hilens.VideoCapture(skill_cfg['IPC_address2']) except Exception as e: hilens.fatal("Failed to create camera2 with {}, e: {}", skill_cfg['IPC_address2'], e) upload_uri2 = skill_cfg['upload_uri2'] initModel(model)
def run(): # 配置系统日志级别 hilens.set_log_level(hilens.ERROR) # 系统初始化,参数要与创建技能时填写的检验值保持一致 hilens.init("gesture") # 初始化模型 gesture_model_path = hilens.get_model_dir() + "gesture_template_model.om" gesture_model = hilens.Model(gesture_model_path) # 初始化本地摄像头与HDMI显示器 camera = hilens.VideoCapture() display_hdmi = hilens.Display(hilens.HDMI) # 上一次上传OBS图片的时间与上传间隔 last_upload_time = 0 upload_duration = 5 # 读取技能配置 skill_cfg = hilens.get_skill_config() if skill_cfg is None or 'server_url' not in skill_cfg: hilens.error("server_url not configured") return while True: # 读取一帧图片(YUV NV21格式) input_yuv = camera.read() # 图片预处理:转为RGB格式、缩放为模型输入尺寸 img_rgb = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2RGB_NV21) img_preprocess, img_w, img_h = preprocess(img_rgb) # 模型推理 output = gesture_model.infer([img_preprocess.flatten()]) # 后处理得到手势所在区域与类别,并在RGB图中画框 bboxes = get_result(output, img_w, img_h) img_rgb = draw_boxes(img_rgb, bboxes) # 输出处理后的图像到HDMI显示器,必须先转回YUV NV21格式 output_yuv = hilens.cvt_color(img_rgb, hilens.RGB2YUV_NV21) display_hdmi.show(output_yuv) # 上传OK手势图片到OBS,为防止OBS数据存储过多,间隔一定的时间才上传图片 if time.time() - last_upload_time > upload_duration: # 截取出OK手势图片(如果有的话) img_OK = get_OK(img_rgb, bboxes) if img_OK is not None: # 上传OK手势图片到OBS,图片(用当前时间命名)需要先转为BGR格式并按照jpg格式编码 img_OK = cv2.cvtColor(img_OK, cv2.COLOR_RGB2BGR) img_OK = cv2.imencode('.jpg', img_OK)[1] filename = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) ret = hilens.upload_bufer(filename + "_OK.jpg", img_OK, "write") if ret != 0: hilens.error("upload pic failed!") return last_upload_time = time.time() # 以POST方式传输处理后的整张图片 try: post_msg(skill_cfg['server_url'], img_rgb) except Exception as e: hilens.error("post data failed!") print("Reason : ", e) hilens.terminate()
def run(): # 系统初始化,参数要与创建技能时填写的检验值保持一致 hilens.init("landmarks") # 初始化自带摄像头与HDMI显示器 camera = hilens.VideoCapture() display = hilens.Display(hilens.HDMI) # 初始化模型:人脸检测模型(centerface)、人脸68个关键点检测模型 centerface_model_path = hilens.get_model_dir() + "centerface_template_model.om" centerface_model = hilens.Model(centerface_model_path) landmark_model_path = hilens.get_model_dir() + "landmark68_template_model.om" landmark_model = hilens.Model(landmark_model_path) # 本段代码展示如何录制HiLens Kit摄像头拍摄的视频 fps = 10 size = (1280, 720) format = cv2.VideoWriter_fourcc('M','J','P','G') # 注意视频格式 writer = cv2.VideoWriter("face.avi", format, fps, size) # 待保存视频的起始帧数,可自行调节或加入更多逻辑 frame_count = 0 frame_start = 100 frame_end = 150 uploaded = False while True: # 读取一帧图片(YUV NV21格式) input_yuv = camera.read() # 图片预处理:转为RGB格式、缩放为模型输入尺寸 img_rgb = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2RGB_NV21) img_pre = preprocess(img_rgb) img_h, img_w = img_rgb.shape[:2] # 人脸检测模型推理,并进行后处理得到画面中最大的人脸检测框 output = centerface_model.infer([img_pre.flatten()]) face_box = get_largest_face_box(output, img_h, img_w) # 画面中检测到有人脸且满足一定条件 if face_box is not None: # 截取出人脸区域并做预处理 img_face = preprocess_landmark(img_rgb, face_box) # 人脸关键点模型推理,得到68个人脸关键点 output2 = landmark_model.infer([img_face.flatten()]) landmarks = output2[0].reshape(68, 2) # 将人脸框和人脸关键点画在RGB图中 img_rgb = draw_landmarks(img_rgb, face_box, landmarks) # 输出处理后的图像到HDMI显示器,必须先转换成YUV NV21格式 output_nv21 = hilens.cvt_color(img_rgb, hilens.RGB2YUV_NV21) display.show(output_nv21) # 录制一段视频并发送到OBS中 if not uploaded: frame_count += 1 if frame_count > frame_end: # 录制结束点 uploaded = True writer.release() # 先保存在本地 ret = hilens.upload_file("face.avi", "face.avi", "write") # 发送到OBS中 if ret != 0: hilens.error("upload file failed!") return elif frame_count > frame_start: # 录制开始点 # 注意写入的图片格式必须为BGR writer.write(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)) hilens.terminate()
import hilens import cv2 import time import math import numpy as np net_h = 352 net_w = 640 class_names = ['plane'] class_num = len(class_names) aipp_flag = True model_path = hilens.get_model_dir() + "convert-plane.om" stride_list = [8, 16, 32] anchors_1 = np.array([[10,13], [16,30], [33,23]]) / stride_list[0] anchors_2 = np.array([[30,61], [62,45], [59,119]]) / stride_list[1] anchors_3 = np.array([[116,90], [156,198], [163,326]]) / stride_list[2] anchor_list = [anchors_1, anchors_2, anchors_3] conf_threshold = 0.3 iou_threshold = 0.4 colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255), (255, 0, 255), (255, 255, 0)] def preprocess(image, aipp_flag=True): image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) img_h, img_w, _ = image.shape