首页 > 解决方案 > 无论如何将videoCapture(0)(来自python文件)放在C#用户界面中

问题描述

我正在做关于人脸识别的项目。我正在研究在 Visual Studio 2019 中使用 C# 创建用户界面

我想知道如何将实时视频输出从videoCapture.pyC# 用户界面

这是代码videoCapture.py

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

这是 UI 的命名空间代码

namespace DropDownMenu
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var menuReceptionist = new List<SubItem>();
            menuReceptionist.Add(new SubItem("Profiles", new UserControlAdd()));
            var item0 = new ItemMenu("Receptionist", menuReceptionist, PackIconKind.ViewDashboard);

            var menuAdmin = new List<SubItem>();
            menuAdmin.Add(new SubItem("Admins Profile", new UserControlAdmins()));
            menuAdmin.Add(new SubItem("Add Admin", new UserControlAdd()));
            var item1 = new ItemMenu("Administrations", menuAdmin, PackIconKind.FaceProfile);

            var menuPreprocess = new List<SubItem>();
            menuPreprocess.Add(new SubItem("New Data", new UserControlAdmins()));
            var item2 = new ItemMenu("Data PreProcessing", menuPreprocess, PackIconKind.Schedule);

            Menu.Children.Add(new UserControlMenuItem(item0, this));
            Menu.Children.Add(new UserControlMenuItem(item1, this));
            Menu.Children.Add(new UserControlMenuItem(item2, this));
        }

        internal void SwitchScreen(object sender)
        {
            var screen = ((UserControl)sender);

            if(screen!=null)
            {
                StackPanelMain.Children.Clear();
                StackPanelMain.Children.Add(screen);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window1 win1 = new Window1();
            win1.Show();
            this.Close();
        }
    }
}

如果我做错了什么,我提前道歉。这是我第一次在 Stackoverflow 上发帖

标签: pythonuser-interfacevisual-studio-2019

解决方案


您需要从 c# 运行您的 python 脚本,这可以通过几种方法来完成。这里有一篇文章解释了这个过程:

从 C# 运行 Python 脚本并处理结果


推荐阅读