女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

把YOLOv11和Python Qt做個用戶界面程序

安費諾傳感器學堂 ? 來源:安費諾傳感器學堂 ? 2024-11-28 10:18 ? 次閱讀

早些時間,小編想把PID控制器優化部分通過Py Qt來實現用戶界面化,不過看著窗口一堆參數,有點發怵:這玩意誰用?。?/p>

9e185014-a936-11ef-93f3-92fbcf53809c.png

參考《PID控制器參數自動優化示例和比較》

后來就擱置了。 在通過Python以及YOLO來檢測圖中或者視頻中的各種物體以及人物的時候,就會考慮:全部用代碼來輸出的?怎么通過一個簡單直觀的窗口界面來測試相應的功能?我覺得要再試一下,哪怕是最簡單的方式呈現。這便是這篇文章的目的所在。 我們通過YOLO現成的各種檢測模塊,實現:

圖中物體的識別檢測

檢出物體的區域分割

人體姿態的檢測

還有其他的檢測功能,感覺功能有重疊,就選了上面的三個功能簡單地集成到一個windows的因為分類的功能只是軟件界面中。 以下圖片均有AI生成,但是人體姿態檢測的結果可能看不清楚輸出。

按照我們之前的慣例,先提供結果,后提供代碼。

9e83512a-a936-11ef-93f3-92fbcf53809c.png

[1]物體檢測

9eb9e1f4-a936-11ef-93f3-92fbcf53809c.png

[2]物體分割

9efb1868-a936-11ef-93f3-92fbcf53809c.png

[3]姿態檢測

我們可以自己嘗試運行一下。

# This program is part of a project developed by Amphenol Sensors.
#Copyright(C)2024,Leo Lu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .


import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, 
                             QFileDialog, QComboBox, QHBoxLayout)
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
import cv2
import numpy as np
import random


from ultralytics import YOLO
from ultralytics.models.yolo.pose.predict import PosePredictor


class YOLOInterface(QWidget):
    def __init__(self):
        super().__init__()


        self.initUI()
        self.model_detec = YOLO('./yolo_models/yolo11s.pt')
        self.model_seg = YOLO('./yolo_models/yolo11s-seg.pt')
        self.model_bd = './yolo_models/yolo11s-pose.pt'
        self.connections = ((2, 4), (1, 3), (10, 8), (8, 6), (6, 5), (5, 7), (7, 9), 
                            (6, 12), (12, 14), (14, 16), (5, 11), (11, 13), (13, 15))


    def initUI(self):
        self.setWindowTitle('YOLO 圖像檢測')
        self.setGeometry(100, 100, 1200, 600)
        self.setFixedSize(1200, 600)
        # 主布局
        main_layout = QVBoxLayout()


        # 圖像顯示布局
        image_layout = QHBoxLayout()


        self.image_label = QLabel(self)
        self.image_label.setFixedSize(600, 400)
        self.image_label.setAlignment(Qt.AlignCenter)
        image_layout.addWidget(self.image_label)


        self.result_label = QLabel(self)
        self.result_label.setFixedSize(600, 400)
        self.result_label.setAlignment(Qt.AlignCenter)
        image_layout.addWidget(self.result_label)


        main_layout.addLayout(image_layout)


        # 控制布局
        control_layout = QHBoxLayout()


        self.detect_button = QPushButton('選擇圖片', self)
        self.detect_button.clicked.connect(self.load_image)
        control_layout.addWidget(self.detect_button)


        self.yolo_combo = QComboBox(self)
        self.yolo_combo.addItems(['物體檢測', '物體分割', '人體姿態識別'])  # 假設在此處添加不同的YOLO任務
        control_layout.addWidget(self.yolo_combo)


        self.run_button = QPushButton('開始檢測', self)
        self.run_button.clicked.connect(self.run_yolo)
        control_layout.addWidget(self.run_button)


        self.quit_button = QPushButton('退出', self)
        self.quit_button.clicked.connect(self.close_application)
        control_layout.addWidget(self.quit_button)


        main_layout.addLayout(control_layout)
        self.setLayout(main_layout)


    def load_image(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "選擇圖片文件", "", "Images (*.png *.jpg *.bmp)", options=options)
        if file_name:
            self.current_image = file_name
            pixmap = QPixmap(file_name)
            scaled_pixmap = pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio)
            self.image_label.setPixmap(scaled_pixmap)


    def plot_keypoints(self, image, keypoints, line_color=(60, 179, 113), point_color=(255, 0, 0),
                   offset=(0, 0), show_idx=False):
        if keypoints is None:
            return image
        
        for data in keypoints.xy:
            if len(data) == 0:
                continue
            # Draw connections between keypoints
            for start_index, end_index in self.connections:
                start_point, end_point = data[start_index], data[end_index]
                if all(start_point[:2] > 0) and all(end_point[:2] > 0):  # Ignore invalid points
                    cv2.line(image, 
                             tuple(map(lambda v, o: int(v + o), start_point[:2], offset)), 
                             tuple(map(lambda v, o: int(v + o), end_point[:2], offset)), 
                             line_color, 2)
            # Draw keypoints
            for index, (x, y) in enumerate(data[:, :2]):
                if x > 0 or y > 0:  # Ignore invalid points
                    cv2.circle(image, 
                               (int(x + offset[0]), int(y + offset[1])), 
                               5, point_color, -1)
                    if show_idx:
                        cv2.putText(image, 
                                    str(index), 
                                    (int(x + offset[0]), int(y + offset[1])), 
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, point_color, 1, cv2.LINE_AA)
    
        return image


    def run_yolo(self):
        if hasattr(self, 'current_image'):
            img = cv2.imread(self.current_image)
            
            # YOLO推理示例:
            task = self.yolo_combo.currentText()
            if task =='物體檢測':
                #model = self.model_detec #YOLO('yolo11s.pt')
                results = self.model_detec(img)
                
                for result in results:
                    boxes = result.boxes                # Pseudo-code; adjust based on actual library


                    for box in boxes:
                        x1, y1, x2, y2 = box.xyxy[0]    # Extracting the bounding box coordinates
                        class_name = self.model_detec.names[int(box.cls[0])]  # Using class_id to get class_name from model
                        confidence = box.conf.item()
                        cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
                        txt_y_pos = int(y1) - 10
                        if txt_y_pos <= 10:
                            txt_y_pos = int(y2) - 10
                        
                        class_name = class_name + " "+ "{:.2g}".format(confidence)
                        cv2.putText(img, class_name, (int(x1), txt_y_pos), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)


            elif task =='物體分割':
                #model = YOLO('yolo11s-seg.pt')
                results = self.model_seg(img)
                
                # Prepare an overlay image with the same dimensions
                overlay = img.copy()
                
                for result in results:
                    boxes = result.boxes  # Pseudo-code; adjust based on actual library
                    masks = result.masks
                    names = result.names
                    for box, mask in zip(boxes, masks):
                        for cls, contour in zip(box.cls, mask.xy):
                            class_id = cls.item()  # Get scalar from tensor
                            class_name = names[class_id]
                            #print(class_name)
                            #print(cv2.contourArea(contour))  # Calculate contour area
                            
                            # Generate a random color
                            color = [random.randint(0, 255) for _ in range(3)]
                            
                            # Fill the contour on the overlay with the random color
                            cv2.drawContours(overlay, [contour.astype(np.int32)], -1, color, thickness=cv2.FILLED)
                # Define the alpha (transparency factor)
                alpha = 0.4  # Value between 0 (transparent) and 1 (opaque)
                
                # Blend the overlay with the original image
                """
                Parameters
                overlay (src1):


                This is the first input array (image).
                In your context, this represents the overlay image, which typically contains modifications like semi-transparent masks drawn over contours.
                
                alpha:
                This is the weight of the first array (image).
                It controls the opacity of the overlay. A value closer to 1 makes the overlay more prominent, while a value closer to 0 makes it less prominent.
                
                img (src2):
                This is the second input array (image).
                It represents the original image onto which the overlay is being applied.
                
                1 - alpha (beta):
                This is the weight of the second array (image).
                Complementary to alpha, it controls the visibility of the original image. A value closer to 1 makes the original image more visible, while closer to 0 makes it less visible.
                
                0 (gamma):
                A scalar added to each sum.
                Typically set to 0 when blending for direct overlay purposes without additional brightness adjustment.
                
                img (dst):
                The destination array where the output is stored.
                It uses the same variable as the original image, implying that the blended result will overwrite this variable.
                """
                cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0, img)


            elif task == '人體姿態識別':
                #model = YOLO('yolo11s-pose.pt')
                #results = model(img)
                
                overrides_Body_pose = {
                    "task": "pose",
                    "mode": "predict",
                    "model": self.model_bd,        #'yolo11s-pose.pt'
                    "verbose": False,
                    "classes": [0],
                    "iou": 0.5,
                    "conf": 0.3
                    }


                predictor_ren_pose = PosePredictor(overrides=overrides_Body_pose)
                pose_ren = predictor_ren_pose(img)[0]
                
                img = self.plot_keypoints(img, pose_ren.keypoints)
                
            # Convert the image to a format suitable for PyQt
            height, width, channel = img.shape
            bytes_per_line = 3 * width
            q_image = QImage(img.data, width, height, bytes_per_line, QImage.Format_RGB888).rgbSwapped()
            pixmap = QPixmap.fromImage(q_image)
            self.display_results(pixmap)


    def display_results(self, pixmap):
        scaled_pixmap = pixmap.scaled(self.result_label.size(), Qt.KeepAspectRatio)
        self.result_label.setPixmap(scaled_pixmap)


    def close_application(self):
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = YOLOInterface()
    ex.show()
    sys.exit(app.exec_())
小編甚至用它來檢測一個AI生成的油畫風格的圖片。看圖中哪些抽象的只有形態沒有細節的人和物體,居然仍然可以檢測出來。大家可以試一下看看檢測的結果。

代碼中涉及到的python軟件包,需要額外安裝,模型文件用的是YOLO現成的。小編把這個軟件用打包工具生成的exe文件,因為攘括的軟件多,簡單的這些功能,打包后的文件壓縮后的大小竟高達近900MB(分割壓縮工具要收費咯),而很多平臺不支持這么大的文件上傳,所以這里也無法提供打包后的文件下載鏈接。

9f681a44-a936-11ef-93f3-92fbcf53809c.png

視線收回,我們心曠神怡一下。感謝AI的提供。

是否在懷疑這個世界的真實性?用傳感器來檢測一下吧,這個世界的冷熱、正負壓,振動,周圍的空氣......可以負責任地告訴各位,我們還是碳基生物。

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 程序
    +關注

    關注

    117

    文章

    3819

    瀏覽量

    82331
  • PID控制器
    +關注

    關注

    2

    文章

    173

    瀏覽量

    19051
  • python
    +關注

    關注

    56

    文章

    4823

    瀏覽量

    86067

原文標題:把YOLOv11和Python Qt做個用戶界面程序

文章出處:【微信號:安費諾傳感器學堂,微信公眾號:安費諾傳感器學堂】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關推薦
    熱點推薦

    labview調用yolov8/11目標檢測、分割、分類

    labview使用2020版本64位編輯,調用yolov8/11的onnx模型案例。 源碼: 通過網盤分享的文件:Labview_cls.zip等4個文件 鏈接: https
    發表于 04-21 19:37

    【技術案例】Qt 環境部署 - Ubuntu 篇

    什么是Qt?Qt是一個跨平臺C++圖形用戶界面應用程序開發框架,可構建高性能的桌面、移動及Web應用程序
    的頭像 發表于 04-16 17:17 ?262次閱讀
    【技術案例】<b class='flag-5'>Qt</b> 環境部署 - Ubuntu 篇

    RK3576 yolov11-seg訓練部署教程

    級的精確目標檢測與分割,適用于自動駕駛、醫學影像、工業檢測等對精度和速度要求苛刻的場景。 ? ? ? ?本教程針對目標分割算法yolov11 seg的訓練和部署到EASY-EAI-Orin-nano(RK3576)進行說明,而數據標注方法可以參考我們往期的文章。
    的頭像 發表于 04-16 09:43 ?436次閱讀
    RK3576 <b class='flag-5'>yolov11</b>-seg訓練部署教程

    OrinNano yolov11訓練部署教程

    ORinNano yolov11訓練部署教程
    的頭像 發表于 04-10 15:26 ?460次閱讀
    OrinNano  <b class='flag-5'>yolov11</b>訓練部署教程

    RK3576 Yolov11訓練部署教程

    YOLO11 系列是 YOLO 家族中最先進的 (SOTA)、最輕量級、最高效的模型,其表現優于其前輩。它由 Ultralytics 創建,該組織發布了 YOLOv8,這是迄今為止最穩定、使用最廣泛的 YOLO 變體。YOLO11
    的頭像 發表于 04-03 09:35 ?557次閱讀
    RK3576 <b class='flag-5'>Yolov11</b>訓練部署教程

    Raspberry Pi上使用IR v11及OpenVINO? 2021.3后不兼容怎么辦?

    使用 Google Colab OpenVINO? 2022.2 將 yolov5 模型轉換為 IR v11: !git clone https://github.com/ultralytics
    發表于 03-06 07:14

    【米爾RK3576開發板評測】+項目名稱YOLOV5目標檢測

    /examples/yolov5/python python yolov5.py --model_path ../model/yolov5s
    發表于 02-15 13:24

    RK3568國產芯教學實驗箱:指紋識別實戰案例

    )GUI的設計實現:LCD顯示界面以及與用戶的交互;(3)編輯控制代碼;(4)編譯程序;(5)運行程序Qt
    的頭像 發表于 01-08 19:05 ?628次閱讀
    RK3568國產芯教學實驗箱:指紋識別實戰案例

    基于QT開發國產主板終端桌面程序 高效、穩定、跨平臺的解決方案

    了開發者們面臨的一個重要挑戰。本文將介紹如何基于QT框架,開發適用于國產主板的終端桌面程序,并探討其優勢和應用場景。 QT是一個跨平臺的C++圖形用戶
    的頭像 發表于 01-08 15:34 ?670次閱讀

    在RK3568教學實驗箱上實現基于YOLOV5的算法物體識別案例詳解

    安裝了所有必要的依賴。這通常包括 torch、torchvision 和 opencv-python。 (2)下載預訓練模型 YOLOv5 提供了多個預訓練模型,可以從官方倉庫或相關資源中下載。 (3
    發表于 12-03 14:56

    YOLOv6在LabVIEW中的推理部署(含源碼)

    YOLOv6 是美團視覺智能部研發的一款目標檢測框架,致力于工業應用。如何使用python進行該模型的部署,官網已經介紹的很清楚了,但是對于如何在LabVIEW中實現該模型的部署,筆者目前還沒有看到
    的頭像 發表于 11-06 16:07 ?804次閱讀
    <b class='flag-5'>YOLOv</b>6在LabVIEW中的推理部署(含源碼)

    國產Cortex-A55實驗箱操作案例分享:LED程序開發

    一、實驗目的(1)熟悉Qt程序的開發流程。(2)掌握QtCreator的基礎開發使用。(3)通過編寫Qt程序實現LED控制的顯示界面。二、實
    的頭像 發表于 11-01 08:10 ?422次閱讀
    國產Cortex-A55實驗箱操作案例分享:LED<b class='flag-5'>程序</b>開發

    EtherCAT運動控制器上位機開發之Python+Qt(三):PDO配置與SDO讀寫

    PC上位機Python+Qt混合編程實現PDO配置與SDO讀寫
    的頭像 發表于 08-21 15:56 ?1757次閱讀
    EtherCAT運動控制器上位機開發之<b class='flag-5'>Python+Qt</b>(三):PDO配置與SDO讀寫

    EtherCAT運動控制器上位機之Python+Qt(一):鏈接與單軸運動

    PC上位機Python+Qt混合編程,助力智能制造高效開發。
    的頭像 發表于 07-31 09:43 ?736次閱讀
    EtherCAT運動控制器上位機之<b class='flag-5'>Python+Qt</b>(一):鏈接與單軸運動

    上位機可視化界面編程軟件有哪些

    。以下是一些常見的上位機可視化界面編程軟件: Qt (跨平臺C++框架) 介紹 :Qt是一個跨平臺的C++圖形用戶界面應用
    的頭像 發表于 06-06 10:48 ?3463次閱讀