{"id":6592,"date":"2025-09-29T11:29:27","date_gmt":"2025-09-29T03:29:27","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=6592"},"modified":"2025-09-29T11:29:35","modified_gmt":"2025-09-29T03:29:35","slug":"yolov8%e6%a1%86%e6%9e%b6%e8%ae%ad%e7%bb%83%e7%ba%a2%e5%a4%96%e5%9b%be%e5%83%8f%e5%b0%8f%e7%9b%ae%e6%a0%87%e6%a3%80%e6%b5%8b%e6%95%b0%e6%8d%ae%e9%9b%86-%e7%ba%a2%e5%a4%96%e5%9b%be%e5%83%8f%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/yolo\/yolov8%e6%a1%86%e6%9e%b6%e8%ae%ad%e7%bb%83%e7%ba%a2%e5%a4%96%e5%9b%be%e5%83%8f%e5%b0%8f%e7%9b%ae%e6%a0%87%e6%a3%80%e6%b5%8b%e6%95%b0%e6%8d%ae%e9%9b%86-%e7%ba%a2%e5%a4%96%e5%9b%be%e5%83%8f%e5%b0%8f\/","title":{"rendered":"YOLOv8\u6846\u67b6\u8bad\u7ec3\u7ea2\u5916\u56fe\u50cf\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6 \u7ea2\u5916\u56fe\u50cf\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6 \u5efa\u7acb\u7ea2\u5916\u5c0f\u76ee\u6807\u68c0\u6d4b\u7cfb\u7edf\u8bc6\u522b\u68c0\u6d4b\u98de\u673a \u65e0\u4eba\u673a \u76f4\u5347\u673a \u9e1f\u7c7b"},"content":{"rendered":"\n<p>\u6df1\u5ea6\u5b66\u4e60\u57fa\u4e8eYOLOv8\u6846\u67b6\u6765\u8bad\u7ec3\u7ea2\u5916\u56fe\u50cf\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6 \u7ea2\u5916\u56fe\u50cf\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6\u7684\u8bad\u7ec3\u53ca\u5e94\u7528 \u63a8\u7406\u8bc6\u522b\u68c0\u6d4b\u98de\u673a \u65e0\u4eba\u673a \u76f4\u5347\u673a \u9e1f\u7c7b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6587\u7ae0\u76ee\u5f55<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e00\u3001\u73af\u5883\u51c6\u5907<\/li>\n\n\n\n<li>\u4e8c\u3001\u6570\u636e\u51c6\u5907\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u96c6\u7ec4\u7ec7<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u4e09\u3001\u8bad\u7ec3\u6a21\u578b\n<ul class=\"wp-block-list\">\n<li>\u8bad\u7ec3\u811a\u672c (`train.py`)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u56db\u3001\u63a8\u7406\u4e0e\u8bc4\u4f30\n<ul class=\"wp-block-list\">\n<li>\u63a8\u7406\u811a\u672c (`detect.py`)<\/li>\n\n\n\n<li>\u8bc4\u4f30\u811a\u672c (`evaluate.py`)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u4e00\u3001\u51c6\u5907<\/li>\n\n\n\n<li>\u4e8c\u3001\u6a21\u578b\u5c01\u88c5\n<ul class=\"wp-block-list\">\n<li>`model_utils.py`<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u4e09\u3001\u6784\u5efa\u547d\u4ee4\u884c\u5de5\u5177\n<ul class=\"wp-block-list\">\n<li>`infrared_detector_cli.py`<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u56db\u3001\u8fd0\u884c\u4f60\u7684\u7cfb\u7edf<\/li>\n<\/ul>\n\n\n\n<p><br><strong>\u6570\u636e\u96c6\u63cf\u8ff0\uff1a<\/strong><br>8302\u5f20\uff0cyolo\/voc\u6807\u6ce8<br>4\u7c7b\uff0c\u6807\u6ce8\u6570\u91cf\uff1a<\/p>\n\n\n\n<p>image num: 8302<\/p>\n\n\n\n<p>\u201c\u7ea2\u5916\u56fe\u50cf\u4e0b\u6781\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6\u201d<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u522b<\/th><th>\u82f1\u6587\u540d\u79f0<\/th><th>\u6807\u6ce8\u6846\u6570\u91cf<\/th><th>\u56fe\u50cf\u6570\u91cf\uff08\u542b\u8be5\u7c7b\uff09<\/th><th>\u603b\u56fe\u50cf\u6570<\/th><th>\u6807\u6ce8\u683c\u5f0f\u652f\u6301<\/th><\/tr><\/thead><tbody><tr><td>\u98de\u673a<\/td><td>Plane<\/td><td>2,163<\/td><td>-<\/td><td><\/td><td>YOLO \u548c VOC (Pascal VOC)<\/td><\/tr><tr><td>\u65e0\u4eba\u673a<\/td><td>Drone<\/td><td>3,120<\/td><td>-<\/td><td><\/td><td>YOLO \u548c VOC (Pascal VOC)<\/td><\/tr><tr><td>\u76f4\u5347\u673a<\/td><td>Heli<\/td><td>2,217<\/td><td>-<\/td><td><\/td><td>YOLO \u548c VOC (Pascal VOC)<\/td><\/tr><tr><td>\u9e1f\u7c7b<\/td><td>Bird<\/td><td>1,958<\/td><td>-<\/td><td>8,302<\/td><td>YOLO \u548c VOC (Pascal VOC)<\/td><\/tr><tr><td><strong>\u603b\u8ba1<\/strong><\/td><td>\u2014\u2014<\/td><td><strong>9,458<\/strong><\/td><td>\u2014\u2014<\/td><td><strong>8,302<\/strong><\/td><td>\u2014\u2014<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><img decoding=\"async\" class=\"lazyload\"alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" data-src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\"><noscript><img decoding=\"async\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0\"><\/noscript><br>1<\/p>\n\n\n\n<p>1<br><img decoding=\"async\" class=\"lazyload\"alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" data-src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKicJO37JCcKiczyLibDpRgBDXlTon1aE9dVcNEEQ2gvaQTRZOajJPzLexw\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=1\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\"><noscript><img decoding=\"async\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKicJO37JCcKiczyLibDpRgBDXlTon1aE9dVcNEEQ2gvaQTRZOajJPzLexw\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=1\"><\/noscript><br>1<br><img decoding=\"async\" class=\"lazyload\"alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" data-src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKAyqwlqJhAhUaqz66Via4WKqzx8qTkKvrURcsQVImhEUmXhZAYbia279Q\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=2\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\"><noscript><img decoding=\"async\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKAyqwlqJhAhUaqz66Via4WKqzx8qTkKvrURcsQVImhEUmXhZAYbia279Q\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=2\"><\/noscript><br>\u7ea2\u5916\u56fe\u50cf\u4e0b\u6781\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6\u8fdb\u884c\u8bad\u7ec3\u3001\u63a8\u7406\u548c\u8bc4\u4f30\uff0c\u57fa\u4e8eYOLOv8\u6846\u67b6\u6765\u5b9e\u73b0\u3002\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u5305\u62ec\u5982\u4f55\u51c6\u5907\u6570\u636e\u3001\u8bad\u7ec3\u6a21\u578b\u3001\u8fdb\u884c\u63a8\u7406\u4ee5\u53ca\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u73af\u5883\u51c6\u5907<\/h3>\n\n\n\n<p>\u786e\u4fdd\u73af\u5883\u4e2d\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip&nbsp;install&nbsp;torch torchvision torchaudio --index-url https:\/\/download.pytorch.org\/whl\/cu118<br>pip&nbsp;install&nbsp;ultralytics opencv-python matplotlib tqdm<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u6570\u636e\u51c6\u5907<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u6570\u636e\u96c6\u7ec4\u7ec7<\/h4>\n\n\n\n<p>\u6570\u636e\u96c6\u6309\u7167\u4ee5\u4e0b\u7ed3\u6784\u7ec4\u7ec7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>infrared_dataset\/<br>\u251c\u2500\u2500 images\/<br>\u2502 &nbsp; \u251c\u2500\u2500 train\/<br>\u2502 &nbsp; \u251c\u2500\u2500 val\/<br>\u2502 &nbsp; \u2514\u2500\u2500 test\/<br>\u251c\u2500\u2500 labels\/<br>\u2502 &nbsp; \u251c\u2500\u2500 train\/<br>\u2502 &nbsp; \u251c\u2500\u2500 val\/<br>\u2502 &nbsp; \u2514\u2500\u2500 test\/<br>\u2514\u2500\u2500 data.yaml<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d&nbsp;<code>data.yaml<\/code>&nbsp;\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>train:&nbsp;.\/infrared_dataset\/images\/train<br>val:&nbsp;.\/infrared_dataset\/images\/val<br>test:&nbsp;.\/infrared_dataset\/images\/test<br><br>nc:4<br>names:&#91;'Plane','Drone','Heli','Bird']<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u6570\u636e\u6807\u6ce8\u4e3aVOC\u683c\u5f0f\uff0c\u8bf7\u5148\u8f6c\u6362\u4e3aYOLO\u683c\u5f0f\uff08\u5373\u6bcf\u4e2a\u6807\u7b7e\u6587\u4ef6\u5305\u542b\u6bcf\u884c\u4e00\u4e2a\u8fb9\u754c\u6846\uff0c\u683c\u5f0f\u4e3a\uff1a\u7c7b\u522bID \u4e2d\u5fc3\u70b9x \u4e2d\u5fc3\u70b9y \u5bbd \u9ad8\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u8bad\u7ec3\u6a21\u578b<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bad\u7ec3\u811a\u672c (<code>train.py<\/code>)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;ultralytics&nbsp;import&nbsp;YOLO<br>import&nbsp;torch<br><br>deftrain_model():<br><em># Check GPU availability<\/em><br>&nbsp; &nbsp; device&nbsp;=0if&nbsp;torch.cuda.is_available()else'cpu'<br>print(f\"Using device:&nbsp;{device}\")<br><br><em># Load pretrained model (YOLOv8s is a good balance between speed and accuracy)<\/em><br>&nbsp; &nbsp; model&nbsp;=&nbsp;YOLO('yolov8s.pt')<br><br><em># Start training<\/em><br>&nbsp; &nbsp; results&nbsp;=&nbsp;model.train(<br>&nbsp; &nbsp; &nbsp; &nbsp; data='infrared_dataset\/data.yaml',<br>&nbsp; &nbsp; &nbsp; &nbsp; epochs=200,<em># Adjust based on your needs<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; imgsz=640,<em># Image size can be adjusted based on the dataset<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; batch=16,<em># Reduce if you encounter out-of-memory issues<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; name='infrared_yolov8s',<br>&nbsp; &nbsp; &nbsp; &nbsp; project='runs\/detect',<br>&nbsp; &nbsp; &nbsp; &nbsp; save=True,<br>&nbsp; &nbsp; &nbsp; &nbsp; save_period=10,<em># Save every 10 epochs<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; device=device,<br>&nbsp; &nbsp; &nbsp; &nbsp; workers=4,<br>&nbsp; &nbsp; &nbsp; &nbsp; patience=50,<em># Early stopping after 50 epochs without improvement<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; optimizer='AdamW',<br>&nbsp; &nbsp; &nbsp; &nbsp; lr0=0.001,<em># Initial learning rate<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; augment=True<em># Enable data augmentation<\/em><br>)<br><br>print(\"Training completed!\")<br>return&nbsp;results<br><br>if&nbsp;__name__&nbsp;=='__main__':<br>&nbsp; &nbsp; train_model()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u63a8\u7406\u4e0e\u8bc4\u4f30<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u63a8\u7406\u811a\u672c (<code>detect.py<\/code>)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;ultralytics&nbsp;import&nbsp;YOLO<br>import&nbsp;cv2<br><br>defdetect_image(image_path,&nbsp;output_path='output_detection.jpg'):<br><em># Load trained model<\/em><br>&nbsp; &nbsp; model&nbsp;=&nbsp;YOLO('runs\/detect\/infrared_yolov8s\/weights\/best.pt')<br><br><em># Perform prediction<\/em><br>&nbsp; &nbsp; results&nbsp;=&nbsp;model.predict(<br>&nbsp; &nbsp; &nbsp; &nbsp; source=image_path,<br>&nbsp; &nbsp; &nbsp; &nbsp; conf=0.4,<em># Confidence threshold<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; iou=0.5,<br>&nbsp; &nbsp; &nbsp; &nbsp; save=True,<br>&nbsp; &nbsp; &nbsp; &nbsp; project='runs\/detect\/predict',<br>&nbsp; &nbsp; &nbsp; &nbsp; name='result',<br>&nbsp; &nbsp; &nbsp; &nbsp; exist_ok=True<br>)<br><br><em># Extract and save results (optional)<\/em><br>for&nbsp;r&nbsp;in&nbsp;results:<br>&nbsp; &nbsp; &nbsp; &nbsp; im&nbsp;=&nbsp;r.plot()<em># Plot bounding boxes and labels<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; cv2.imwrite(output_path,&nbsp;im)<br>print(f\"Detection completed, saved to&nbsp;{output_path}\")<br><br><em># Print detected objects<\/em><br>for&nbsp;box&nbsp;in&nbsp;r.boxes:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls_name&nbsp;=&nbsp;model.names&#91;int(box.cls)]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conf&nbsp;=float(box.conf)<br>print(f\"Detected:&nbsp;{cls_name}&nbsp;(Confidence:&nbsp;{conf:.3f})\")<br><br>return&nbsp;output_path<br><br>if&nbsp;__name__&nbsp;=='__main__':<br>&nbsp; &nbsp; detect_image('test_infrared_image.jpg')<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bc4\u4f30\u811a\u672c (<code>evaluate.py<\/code>)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;ultralytics&nbsp;import&nbsp;YOLO<br><br>defevaluate_model():<br>&nbsp; &nbsp; model&nbsp;=&nbsp;YOLO('runs\/detect\/infrared_yolov8s\/weights\/best.pt')<br><br><em># Evaluate on validation set<\/em><br>&nbsp; &nbsp; metrics&nbsp;=&nbsp;model.val(<br>&nbsp; &nbsp; &nbsp; &nbsp; data='infrared_dataset\/data.yaml',<br>&nbsp; &nbsp; &nbsp; &nbsp; split='val',<em># Or 'test' if evaluating on the test set<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; batch=16,<br>&nbsp; &nbsp; &nbsp; &nbsp; imgsz=640,<br>&nbsp; &nbsp; &nbsp; &nbsp; device=0if&nbsp;torch.cuda.is_available()else'cpu',<br>&nbsp; &nbsp; &nbsp; &nbsp; save_json=True,<em># Save COCO format results for further analysis<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; conf_thres=0.001,<br>&nbsp; &nbsp; &nbsp; &nbsp; iou_thres=0.6<br>)<br><br><em># Output key metrics<\/em><br>print(f\"mAP@0.5:&nbsp;{metrics.box.map50:.4f}\")<br>print(f\"mAP@0.5:0.95:&nbsp;{metrics.box.map:.4f}\")<br>print(f\"Precision:&nbsp;{metrics.box.p:.4f}\")<br>print(f\"Recall:&nbsp;{metrics.box.r:.4f}\")<br><br><em># Output AP per class<\/em><br>print(\"\\nAP per class:\")<br>for&nbsp;i,&nbsp;name&nbsp;inenumerate(metrics.names.values()):<br>print(f\" &nbsp;{name}:&nbsp;{metrics.box.ap&#91;i]:.4f}\")<br><br>if&nbsp;__name__&nbsp;=='__main__':<br>&nbsp; &nbsp; evaluate_model()<\/code><\/pre>\n\n\n\n<p>\u6784\u5efa\u4e00\u4e2a\u53ef\u4ee5\u52a0\u8f7d\u6a21\u578b\u3001\u6267\u884c\u63a8\u7406\u5e76\u5bf9\u65b0\u56fe\u50cf\u6216\u89c6\u9891\u8fdb\u884c\u5b9e\u65f6\u5904\u7406\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u51c6\u5907<\/h3>\n\n\n\n<p>\u786e\u4fdd\u5df2\u7ecf\u5b8c\u6210\u4e86\u6a21\u578b\u7684\u8bad\u7ec3\uff0c\u5e76\u4e14\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u6587\u4ef6\uff08\u4f8b\u5982&nbsp;<code>best.pt<\/code>\uff09\uff0c\u4f4d\u4e8e&nbsp;<code>runs\/detect\/infrared_yolov8s\/weights\/best.pt<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u6a21\u578b\u5c01\u88c5<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5c01\u88c5\u6211\u4eec\u7684\u6a21\u578b\uff0c\u4f7f\u5176\u6613\u4e8e\u4f7f\u7528\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u5b9e\u73b0\uff0c\u8be5\u7c7b\u8d1f\u8d23\u52a0\u8f7d\u6a21\u578b\u5e76\u63d0\u4f9b\u9884\u6d4b\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>model_utils.py<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;ultralytics&nbsp;import&nbsp;YOLO<br>import&nbsp;cv2<br><br>classInfraredTargetDetector:<br>def__init__(self,&nbsp;model_path='runs\/detect\/infrared_yolov8s\/weights\/best.pt'):<br>&nbsp; &nbsp; &nbsp; &nbsp; self.model&nbsp;=&nbsp;YOLO(model_path)<br><br>defpredict(self,&nbsp;image_path,&nbsp;conf=0.4):<br>\"\"\"<br>&nbsp; &nbsp; &nbsp; &nbsp; \u5bf9\u7ed9\u5b9a\u8def\u5f84\u7684\u56fe\u50cf\u8fdb\u884c\u9884\u6d4b\u3002<br><br>&nbsp; &nbsp; &nbsp; &nbsp; :param image_path: \u8f93\u5165\u56fe\u50cf\u7684\u8def\u5f84<br>&nbsp; &nbsp; &nbsp; &nbsp; :param conf: \u7f6e\u4fe1\u5ea6\u9608\u503c<br>&nbsp; &nbsp; &nbsp; &nbsp; :return: \u68c0\u6d4b\u7ed3\u679c\u5217\u8868\uff0c\u6bcf\u4e2a\u7ed3\u679c\u5305\u542b\u7c7b\u522b\u540d\u79f0\u3001\u7f6e\u4fe1\u5ea6\u5206\u6570\u548c\u8fb9\u754c\u6846\u5750\u6807<br>&nbsp; &nbsp; &nbsp; &nbsp; \"\"\"<br>&nbsp; &nbsp; &nbsp; &nbsp; results&nbsp;=&nbsp;self.model.predict(source=image_path,&nbsp;conf=conf)<br>&nbsp; &nbsp; &nbsp; &nbsp; detections&nbsp;=&#91;]<br>for&nbsp;r&nbsp;in&nbsp;results:<br>for&nbsp;box&nbsp;in&nbsp;r.boxes:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls_id&nbsp;=int(box.cls)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label&nbsp;=&nbsp;self.model.names&#91;cls_id]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; confidence&nbsp;=float(box.conf)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bbox&nbsp;=&nbsp;box.xyxy&#91;0].cpu().numpy().tolist()<em># &#91;x1, y1, x2, y2]<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; detections.append({<br>'class':&nbsp;label,<br>'confidence':&nbsp;confidence,<br>'bbox':&nbsp;bbox<br>})<br>return&nbsp;detections<br><br>defdetect_and_draw(self,&nbsp;image_path,&nbsp;output_path='output_detection.jpg',&nbsp;conf=0.4):<br>\"\"\"<br>&nbsp; &nbsp; &nbsp; &nbsp; \u5bf9\u56fe\u50cf\u8fdb\u884c\u68c0\u6d4b\u5e76\u5728\u539f\u56fe\u4e0a\u7ed8\u5236\u68c0\u6d4b\u7ed3\u679c\u3002<br><br>&nbsp; &nbsp; &nbsp; &nbsp; :param image_path: \u8f93\u5165\u56fe\u50cf\u7684\u8def\u5f84<br>&nbsp; &nbsp; &nbsp; &nbsp; :param output_path: \u8f93\u51fa\u56fe\u50cf\u7684\u4fdd\u5b58\u8def\u5f84<br>&nbsp; &nbsp; &nbsp; &nbsp; :param conf: \u7f6e\u4fe1\u5ea6\u9608\u503c<br>&nbsp; &nbsp; &nbsp; &nbsp; :return: None<br>&nbsp; &nbsp; &nbsp; &nbsp; \"\"\"<br>&nbsp; &nbsp; &nbsp; &nbsp; detections&nbsp;=&nbsp;self.predict(image_path,&nbsp;conf)<br>&nbsp; &nbsp; &nbsp; &nbsp; img&nbsp;=&nbsp;cv2.imread(image_path)<br>for&nbsp;det&nbsp;in&nbsp;detections:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bbox&nbsp;=&nbsp;det&#91;'bbox']<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label&nbsp;=f\"{det&#91;'class']}:&nbsp;{det&#91;'confidence']:.2f}\"<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color&nbsp;=(0,255,0)<em># Green<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv2.rectangle(img,(int(bbox&#91;0]),int(bbox&#91;1])),(int(bbox&#91;2]),int(bbox&#91;3])),&nbsp;color,2)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv2.putText(img,&nbsp;label,(int(bbox&#91;0]),int(bbox&#91;1])-10),&nbsp;cv2.FONT_HERSHEY_SIMPLEX,0.9,&nbsp;color,2)<br>&nbsp; &nbsp; &nbsp; &nbsp; cv2.imwrite(output_path,&nbsp;img)<br>print(f\"Detection completed and saved to&nbsp;{output_path}\")<br><br><em># \u793a\u4f8b\u4f7f\u7528<\/em><br>if&nbsp;__name__&nbsp;=='__main__':<br>&nbsp; &nbsp; detector&nbsp;=&nbsp;InfraredTargetDetector()<br>&nbsp; &nbsp; detector.detect_and_draw('test_infrared_image.jpg')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u6784\u5efa\u547d\u4ee4\u884c\u5de5\u5177<\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u547d\u4ee4\u884c\u5bf9\u56fe\u50cf\u6216\u89c6\u9891\u8fdb\u884c\u68c0\u6d4b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><code>infrared_detector_cli.py<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;argparse<br>from&nbsp;model_utils&nbsp;import&nbsp;InfraredTargetDetector<br>import&nbsp;cv2<br><br>defmain():<br>&nbsp; &nbsp; parser&nbsp;=&nbsp;argparse.ArgumentParser(description=\"Infrared Target Detection System\")<br>&nbsp; &nbsp; parser.add_argument('--input',type=str,&nbsp;required=True,help='Path to input image or video file.')<br>&nbsp; &nbsp; parser.add_argument('--output',type=str,&nbsp;default='output.jpg',help='Path to save the output.')<br>&nbsp; &nbsp; parser.add_argument('--conf',type=float,&nbsp;default=0.4,help='Confidence threshold.')<br><br>&nbsp; &nbsp; args&nbsp;=&nbsp;parser.parse_args()<br><br>&nbsp; &nbsp; detector&nbsp;=&nbsp;InfraredTargetDetector()<br><br>if&nbsp;args.input.lower().endswith(('jpg','jpeg','png')):<br>&nbsp; &nbsp; &nbsp; &nbsp; detector.detect_and_draw(args.input,&nbsp;args.output,&nbsp;args.conf)<br>elif&nbsp;args.input.lower().endswith(('mp4','avi')):<br>&nbsp; &nbsp; &nbsp; &nbsp; cap&nbsp;=&nbsp;cv2.VideoCapture(args.input)<br>&nbsp; &nbsp; &nbsp; &nbsp; frame_width&nbsp;=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))<br>&nbsp; &nbsp; &nbsp; &nbsp; frame_height&nbsp;=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))<br>&nbsp; &nbsp; &nbsp; &nbsp; out&nbsp;=&nbsp;cv2.VideoWriter(args.output,&nbsp;cv2.VideoWriter_fourcc(*'XVID'),20.0,(frame_width,&nbsp;frame_height))<br><br>while&nbsp;cap.isOpened():<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret,&nbsp;frame&nbsp;=&nbsp;cap.read()<br>ifnot&nbsp;ret:<br>break<br><br><em># Convert frame to image path temporarily for prediction<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp_img_path&nbsp;='temp_frame.jpg'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv2.imwrite(temp_img_path,&nbsp;frame)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; detections&nbsp;=&nbsp;detector.predict(temp_img_path,&nbsp;args.conf)<br><br>for&nbsp;det&nbsp;in&nbsp;detections:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bbox&nbsp;=&nbsp;det&#91;'bbox']<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label&nbsp;=f\"{det&#91;'class']}:&nbsp;{det&#91;'confidence']:.2f}\"<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color&nbsp;=(0,255,0)<em># Green<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv2.rectangle(frame,(int(bbox&#91;0]),int(bbox&#91;1])),(int(bbox&#91;2]),int(bbox&#91;3])),&nbsp;color,2)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv2.putText(frame,&nbsp;label,(int(bbox&#91;0]),int(bbox&#91;1])-10),&nbsp;cv2.FONT_HERSHEY_SIMPLEX,0.9,&nbsp;color,2)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(frame)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; cap.release()<br>&nbsp; &nbsp; &nbsp; &nbsp; out.release()<br>print(f\"Video detection completed and saved to&nbsp;{args.output}\")<br>else:<br>print(\"Unsupported file format.\")<br><br>if&nbsp;__name__&nbsp;==\"__main__\":<br>&nbsp; &nbsp; main()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u8fd0\u884c\u4f60\u7684\u7cfb\u7edf<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5bf9\u4e8e\u5355\u5f20\u56fe\u7247<\/strong>\uff1a<code>python infrared_detector_cli.py --input path\/to\/image.jpg --output output.jpg --conf\u00a00.5<\/code><\/li>\n\n\n\n<li><strong>\u5bf9\u4e8e\u89c6\u9891\u6587\u4ef6<\/strong>\uff1a<code>python infrared_detector_cli.py --input path\/to\/video.mp4 --output output_video.avi --con<\/code><\/li>\n<\/ul>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5ea6\u5b66\u4e60\u57fa\u4e8eYOLOv8\u6846\u67b6\u6765\u8bad\u7ec3\u7ea2\u5916\u56fe\u50cf\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6 \u7ea2\u5916\u56fe\u50cf\u5c0f\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6\u7684\u8bad\u7ec3\u53ca\u5e94\u7528 \u63a8\u7406\u8bc6\u522b\u68c0\u6d4b\u98de\u673a \u65e0\u4eba\u673a \u76f4\u5347\u673a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[46],"tags":[],"class_list":["post-6592","post","type-post","status-publish","format-standard","hentry","category-yolo"],"post_thumbnail_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","content_first_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_medium_image_300":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_thumbnail_image_624":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_frist_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_medium_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_large_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_full_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_all_images":[{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","id":"image0"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKApoG7syjtSb77WzKMbrdlxyCOicUJuXdbfrqNsQice5dSrtdurSVpdRQ\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","id":"image1"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKicJO37JCcKiczyLibDpRgBDXlTon1aE9dVcNEEQ2gvaQTRZOajJPzLexw\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=1","id":"image2"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKicJO37JCcKiczyLibDpRgBDXlTon1aE9dVcNEEQ2gvaQTRZOajJPzLexw\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=1","id":"image3"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKAyqwlqJhAhUaqz66Via4WKqzx8qTkKvrURcsQVImhEUmXhZAYbia279Q\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=2","id":"image4"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/3aq7xqHVYu3lOF8mVaLAO3D2XLuLXDCKAyqwlqJhAhUaqz66Via4WKqzx8qTkKvrURcsQVImhEUmXhZAYbia279Q\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=2","id":"image5"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"YOLO","post_date":"2025-09-29","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":269,"next_post_id":6657,"next_post_title":"\u6df1\u5ea6\u5b66\u4e60\u76ee\u6807\u68c0\u6d4b YOLOV11\u5982\u4f55\u8bad\u7ec3\u81ea\u5df1\u7684\u6570\u636e\u96c6 YOLOv11\u8bad\u7ec3\u914d\u7f6e\u73af\u5883\u6559\u7a0b \u4ee5\u6c14\u7403\u68c0\u6d4b\u6570\u636e\u96c6\u4e3a\u4f8b","previous_post_id":6590,"previous_post_title":"YOLOV8\u6a21\u578b\u5982\u4f55\u8bad\u7ec3 \u53cc\u6a21\u6001\u7ea2\u5916-\u53ef\u89c1\u5149\u5bf9\u9f50\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6 \u7ea2\u5916\u76ee\u6807\u68c0\u6d4b\u6570\u636e\u96c6\u7684\u8bad\u7ec3\u53ca\u5e94\u7528 \u53cc\u6a21\u6001\uff08\u7ea2\u5916+\u53ef\u89c1\u5149\uff09\u6570\u636e\u96c6","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6592"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6592\/revisions"}],"predecessor-version":[{"id":6593,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6592\/revisions\/6593"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}