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 run(work_path): # 系统初始化,参数要与创建技能时填写的检验值保持一致 hilens.init("driving") # 初始化自带摄像头与HDMI显示器, # hilens studio中VideoCapture如果不填写参数,则默认读取test/camera0.mp4文件, # 在hilens kit中不填写参数则读取本地摄像头 camera = hilens.VideoCapture() display = hilens.Display(hilens.HDMI) # 初始化模型 model_path = os.path.join(work_path, 'model/yolo3.om') driving_model = hilens.Model(model_path) frame_index = 0 json_bbox_list = [] json_data = {'info': 'det_result'} while True: frame_index += 1 try: time_start = time.time() # 1. 设备接入 ##### input_yuv = camera.read() # 读取一帧图片(YUV NV21格式) # 2. 数据预处理 ##### img_bgr = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2BGR_NV21) # 转为BGR格式 img_preprocess, img_w, img_h = preprocess(img_bgr) # 缩放为模型输入尺寸 # 3. 模型推理 ##### output = driving_model.infer([img_preprocess.flatten()]) # 4. 获取检测结果 ##### bboxes = get_result(output, img_w, img_h) # 5-1. [比赛提交作品用] 将结果输出到json文件中 ##### if len(bboxes) > 0: json_bbox = convert_to_json(bboxes, frame_index) json_bbox_list.append(json_bbox) # 5-2. [调试用] 将结果输出到模拟器中 ##### img_bgr = draw_boxes(img_bgr, bboxes) # 在图像上画框 output_yuv = hilens.cvt_color(img_bgr, hilens.BGR2YUV_NV21) display.show(output_yuv) # 显示到屏幕上 time_frame = 1000 * (time.time() - time_start) hilens.info('----- time_frame = %.2fms -----' % time_frame) except RuntimeError: print('last frame') break # 保存检测结果 hilens.info('write json result to file') result_filename = './result.json' json_data['result'] = json_bbox_list save_json_to_file(json_data, result_filename) hilens.terminate()
def run(): # 系统初始化,参数要与创建技能时填写的检验值保持一致 hilens.init("helmet") # 读取技能配置 skill_cfg = hilens.get_skill_config() if skill_cfg is None or 'server_url' not in skill_cfg or 'IPC_address' not in skill_cfg: hilens.error("skill config not correct") return # 获取POST服务器地址和IPC地址,多个IPC地址用分号分隔开 server_url = skill_cfg['server_url'] camera_list = skill_cfg['IPC_address'].split(';') # 每个IPC启动一个独立的线程 threads_list = [] for camera_address in camera_list: t = threading.Thread(target=camera_thread, args=(camera_address, server_url)) t.start() threads_list.append(t) for t in threads_list: t.join() hilens.terminate()
def image_test(): ret = hilens.init("") if ret != 0: hilens.error("Failed to initialize HiLens") return img_file = './flight_test1.jpg' test_img(img_file, model_path) hilens.terminate()
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(self): thread_lists = [self.__cam_thread, self.__pp_thread] if self.__socket_enable: thread_lists.append(self.__socket_3399) for t in thread_lists: t.setDaemon(True) print("start") try: for t in thread_lists: t.start() # Wait for complete. while True: time.sleep(3) for t in thread_lists: t.join() except KeyboardInterrupt: print("KeyboardInterrupt") if self.__hilens_enable: hilens.terminate()
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()
if flag == 1: display_hdmi.show(output_nv21) if stable: print('stable, send data') sendData(upload_uri, res) sleep(3) stable = False num_stable_frames = 0 def runThreads(): cam_thread1 = threading.Thread(target=capVideo, args=(camera1, 0, upload_uri1)) cam_thread1.start() cam_thread1.join() global camera2 if camera2 != None: cam_thread2 = threading.Thread(target=capVideo, args=(camera2, 1, upload_uri2)) cam_thread2.start() cam_thread2.join() if __name__ == '__main__': init() runThreads() hilens.terminate()
def run(work_path): global data # 系统初始化,参数要与创建技能时填写的检验值保持一致 hilens.init("driving") # 初始化自带摄像头与HDMI显示器, # hilens studio中VideoCapture如果不填写参数,则默认读取test/camera0.mp4文件, # 在hilens kit中不填写参数则读取本地摄像头 camera = hilens.VideoCapture() display = hilens.Display(hilens.HDMI) if rec: rec_video(camera, display, show) # 初始化模型 # -*- coding: utf-8 -*- # model_path = os.path.join(work_path, 'model/yolo3_darknet53_raw3_4_sup_slope_terminal_t.om') model_path = os.path.join(work_path, 'model/yolo3_darknet53_raw3_4_sup_slope_now_terminal_t.om') driving_model = hilens.Model(model_path) frame_index = 0 json_bbox_list = [] json_data = {'info': 'det_result'} while True: frame_index += 1 try: time_start = time.time() # 1. 设备接入 ##### input_yuv = camera.read() # 读取一帧图片(YUV NV21格式) # 2. 数据预处理 ##### if rgb: img_rgb = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2RGB_NV21) # 转为RGB格式 else: img_rgb = cv2.cvtColor(input_yuv, cv2.COLOR_YUV2BGR_NV21) # 转为BGR格式 if pad: img_preprocess, img_w, img_h, new_w, new_h, shift_x_ratio, shift_y_ratio = preprocess_with_pad(img_rgb) # 缩放为模型输入尺寸 # 3. 模型推理 ##### output = driving_model.infer([img_preprocess.flatten()]) # 4. 获取检测结果 ##### bboxes = get_result_with_pad(output, img_w, img_h, new_w, new_h, shift_x_ratio, shift_y_ratio) else: img_preprocess, img_w, img_h = preprocess(img_rgb) # 缩放为模型输入尺寸 # 3. 模型推理 ##### output = driving_model.infer([img_preprocess.flatten()]) # 4. 获取检测结果 ##### bboxes = get_result(output, img_w, img_h) # # 5-1. [比赛提交作品用] 将结果输出到json文件中 ##### # if len(bboxes) > 0: # json_bbox = convert_to_json(bboxes, frame_index) # json_bbox_list.append(json_bbox) # # if bboxes != []: # # print() if socket_use: data = data_generate_4(bboxes) # 5-2. [调试用] 将结果输出到display ##### if show: if rgb: img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR) else: img_bgr = img_rgb img_bgr, labelName = draw_boxes(img_bgr, bboxes) # 在图像上画框 output_yuv = hilens.cvt_color(img_bgr, hilens.BGR2YUV_NV21) display.show(output_yuv) # 显示到屏幕上 if log: time_frame = 1000 * (time.time() - time_start) hilens.info('----- time_frame = %.2fms -----' % time_frame) except RuntimeError: print('last frame') break # 保存检测结果 hilens.info('write json result to file') result_filename = './result.json' json_data['result'] = json_bbox_list save_json_to_file(json_data, result_filename) hilens.terminate()