neuralmagic / deepsparse

Sparsity-aware deep learning inference runtime for CPUs
https://neuralmagic.com/deepsparse/
Other
2.99k stars 173 forks source link

How can I use deepsparse instead of ultralytics #1650

Closed piaoyaoi closed 4 months ago

piaoyaoi commented 4 months ago

`def process_frame(frame, contours, pts, object_code, model, class_thresh: int = 0.25, _algorithm_queue: multiprocessing.Queue = None): results = model.predict(source=frame, conf=class_thresh, vid_stride=True, classes=object_code, device=device) # 对当前帧进行目标检测并显示结果 cpu_res = results[0].cpu() boxes = cpu_res.boxes # Boxes object for bbox outputs cls_list = boxes.cls.tolist()

类名字典

names_dic = cpu_res.names

have_algorithm = False
# 循环遍历所有的检测结果
for i in range(len(cls_list)):
    x1, y1, x2, y2 = map(int, boxes.xyxy[i])  # 转换为整数坐标
    # 边界框的四个顶点
    box_points = np.array([
        [x1, y1],
        [x2, y1],
        [x2, y2],
        [x1, y2]
    ], dtype=np.int32)

    # 使用pointPolygonTest检查至少一个边界框的顶点是否在区域内
    is_inside = False
    color = (0, 0, 255)  # 默认红色
    for box_point in box_points:
        # 直接从box_point数组中提取坐标,确保它们作为单独的元素传入
        # is_inside = cv2.pointPolygonTest(pts, (box_point[0], box_point[1]), False) >= 0
        # 显式地将box_point的坐标转换为整数元组
        point_tuple = tuple(map(int, box_point.tolist()))

        is_inside = cv2.pointPolygonTest(pts, point_tuple, False) >= 0
        if is_inside:
            break

    if is_inside:
        have_algorithm = True
    else:
        pass
    if not is_inside:
        color = (0, 255, 0)  # 绿色
        # continue
    # 获取类别名称
    cls_name = names_dic[cls_list[i]]
    cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
    conf = float(boxes.conf[i])
    # 添加文字标签(类别+置信度)
    label = f"{cls_name}: {conf:.2f}"
    cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

cv2.polylines(frame, contours, isClosed=True, color=(0, 255, 0), thickness=2)
# 报警视频帧存入报警队列
if have_algorithm:
    try:
        _algorithm_queue.put_nowait(frame)
    except queue.Full:
        logging.log(logging.ERROR, "Algorithm queue is full")
        pass
return frame

` This is the code for yolov8 that I used earlier,Now I'm going to use deepsparse instead

`def process_frame(frame, contours, pts, object_code, model, yolo_pipeline, class_thresh: int = 0.25, _algorithm_queue: multiprocessing.Queue = None): print('开始检测')

input_tensor = preprocess_frame_for_deepsparse(frame)

# pipeline_outputs = compiled_model.run([input_tensor])
# save_pipeline_outputs_to_file(pipeline_outputs)
# print('pipeline_outputs',pipeline_outputs)
start_time = time.time()
pipeline_outputs_low_conf = yolo_pipeline(images=frame, iou_thres=0.3, conf_thres=0.1)
end_time = time.time()
execution_time = end_time - start_time
print("执行时间:", execution_time*1000, "ms")
boxes = pipeline_outputs_low_conf.boxes
scores = pipeline_outputs_low_conf.scores
labels = pipeline_outputs_low_conf.labels

print('boxes', boxes)
print('scores', scores)
print('labels', labels)`

What should I write
jeanniefinks commented 4 months ago

Hi @piaoyaoi The code looks fine overall and includes the DeepSparse code.... Was there a bug you had encountered or something further you are trying to accomplish?

Best, Jeannie / Neural Magic

piaoyaoi commented 4 months ago

Hi @piaoyaoi The code looks fine overall and includes the DeepSparse code.... Was there a bug you had encountered or something further you are trying to accomplish?

Best, Jeannie / Neural Magic

It was a problem with the version. It's been fixed. Thank you