You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
exercise_2/src/SanDCJ/video.py

340 lines
13 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import logging
import os
import shutil
import PIL
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
import matplotlib
import sys
import cv2
from djitellopy import tello
import matplotlib.pyplot as plt
# matplotlib.figure 模块提供了顶层的Artist(图中的所有可见元素都是Artist的子类)它包含了所有的plot元素
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg # pyqt5的画布
import calib_RGB
import huidu_shuai,qingxi_shuai,xiangsu_shuai,video_to_photo
import argparse
class MyMatplotlibFigure(FigureCanvasQTAgg):
"""
创建一个画布类并把画布放到FigureCanvasQTAgg
"""
def __init__(self, width=10, heigh=10, dpi=100):
plt.rcParams['figure.facecolor'] = 'r' # 设置窗体颜色
plt.rcParams['axes.facecolor'] = 'b' # 设置绘图区颜色
self.width_ = width
self.heigh_ = heigh
self.dpi = dpi
self.figs = Figure(figsize=(self.width_, self.heigh_), dpi=self.dpi)
super(MyMatplotlibFigure, self).__init__(self.figs) # 在父类种激活self.fig 否则不能显示图像
#self.axes = self.figs.add_subplot(111,projection='3d')
fig = plt.figure()
#fig.suptitle('3D reconstructed', fontsize=16)
#self.axes = fig.gca(projection='3d')
self.axes=fig.add_subplot(111, projection='3d')
def mat_plot_drow_axes(self, t, s, l):
"""
用清除画布刷新的方法绘图
:return:
"""
self.axes.cla() # 清除绘图区
self.axes.spines['top'].set_visible(False) # 顶边界不可见
self.axes.spines['right'].set_visible(False) # 右边界不可见
# fig = plt.figure()
# fig.suptitle('3D reconstructed', fontsize=16)
# ax = plt.axes(projection='3d')
# 设置左、下边界在00处相交
# self.axes.spines['bottom'].set_position(('data', 0)) # 设置y轴线原点数据为 0
self.axes.spines['left'].set_position(('data', 0)) # 设置x轴线原点数据为 0
# self.axes.plot(t, s, l, 'b.')
# self.set_xlabel('x axis')
# self.set_ylabel('y axis')
# self.set_zlabel('z axis')
# self.view_init(elev=135, azim=90)
# plt.savefig('Reconstruction.jpg')
# plt.show()
# self.axes.plot(t, s, l, 'o-r', linewidth=0.5)
self.axes.scatter(t,s,l, 'b.')
#self.axes.plot_surface(t,s,l)
self.axes.set_xlabel('x axis')
self.axes.set_ylabel('y axis')
self.axes.set_zlabel('z axis')
self.axes.view_init(elev=135, azim=90)
plt.savefig('Reconstruction.jpg')
plt.show()
self.figs.canvas.draw() # 这里注意是画布重绘self.figs.canvas
self.figs.canvas.flush_events() # 画布刷新self.figs.canvas
class Ui_MainWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.timer_camera = QtCore.QTimer()
self.cap = cv2.VideoCapture()
self.CAM_NUM = 0
self.set_ui()
self.slot_init()
def set_ui(self):
self.setWindowTitle('虚拟沙盘') # 标题
self.resize(1200, 800) # 窗口尺寸
#self.status = self.statusTip('虚拟沙盘3D建模系统') # 消息
#self.status.showMessage()
self.setWindowIcon(QIcon("./img/mou.ico")) # 图标
self.drawn_back()
self.__layout_main = QtWidgets.QVBoxLayout()
self.__layout_fun_button = QtWidgets.QHBoxLayout()
self.__layout_data_show = QtWidgets.QHBoxLayout()
self.button_open_camera = QtWidgets.QPushButton('打开摄像头')
self.button_operate = QtWidgets.QPushButton('键盘控制')
self.button_takephoto = QtWidgets.QPushButton('视频转图片')
self.button_make = QtWidgets.QPushButton('建模')
self.button_close = QtWidgets.QPushButton('退出')
self.button_open_camera.setMinimumHeight(50)
self.button_close.setMinimumHeight(50)
self.button_operate.setMinimumHeight(50)
self.button_takephoto.setMinimumHeight(50)
self.button_make.setMinimumHeight(50)
self.button_open_camera.move(10, 100)
self.label_show_camera = QtWidgets.QLabel()
#self.label_show_camera.move(400,400)
self.label_show_camera.setFixedSize(841, 681)
self.__layout_fun_button.addWidget(self.button_open_camera)
self.__layout_fun_button.addWidget(self.button_operate)
self.__layout_fun_button.addWidget(self.button_takephoto)
self.__layout_fun_button.addWidget(self.button_make)
self.__layout_fun_button.addWidget(self.button_close)
self.__layout_main.addLayout(self.__layout_fun_button)
self.__layout_main.addWidget(self.label_show_camera)
self.setLayout(self.__layout_main)
##########################################
# 将信号与槽关联
#self.btn1.clicked.connect(self.onClick_Button)
#self.btn2.clicked.connect(self.showDialog)
def slot_init(self):
self.button_open_camera.clicked.connect(
self.button_open_camera_clicked)
self.timer_camera.timeout.connect(self.show_camera)
self.button_close.clicked.connect(self.onClick_Button)
self.button_make.clicked.connect(self.showDialog)
self.button_takephoto.clicked.connect(self.take_photo)
self.button_operate.clicked.connect(self.keyboard_control)
def drawn_back(self):
self.palette = QPalette()
self.palette.setBrush(QPalette.Background, QBrush(QPixmap("./img/back.jpeg")))
self.setPalette(self.palette)
def keyboard_control(self):
dialog = QtWidgets.QDialog()
dialog.resize(400, 300)
# button = QtWidgets.QPushButton('储存', dialog)
# button.clicked.connect(dialog.close)
# button.move(350, 550)
dialog.setWindowTitle('键盘控制')
dialog.setWindowModality(QtCore.Qt.ApplicationModal)
# dialog.__layout_main = QtWidgets.QHBoxLayout()
# dialog.__layout_fun_button = QtWidgets.QVBoxLayout()
# dialog.__layout_data_show = QtWidgets.QVBoxLayout()
# dialog.button_takeoff = QtWidgets.QPushButton('起飞')
# dialog.button_set_down = QtWidgets.QPushButton('降落')
# dialog.button.takeoff.setMinimumHeight(50)
# dialog.button_takeoff.move(250, 200)
# dialog.button_takeoff.setMinimumHeight(50)
# dialog.button_set_down.setMinimumHeight(50)
#
# dialog.button_takeoff.move(10, 100)
button_takeoff = QtWidgets.QPushButton('起飞', dialog)
button_takeoff.move(200, 100)
button_set_down = QtWidgets.QPushButton('降落', dialog)
button_set_down.move(200, 150)
lab1 = QLabel()
# lab1.resize(200, 200) # 重设Label大小
# lab1.setScaledContents(True) # 设置图片自适应窗口大小
lab1.setPixmap(QPixmap("./img/control.png"))
vbox = QVBoxLayout()
vbox.addWidget(lab1)
dialog.setLayout(vbox)
#dialog.label_show_camera = QtWidgets.QLabel()
# self.label_show_camera.move(400,400)
#dialog.label_show_camera.setFixedSize(841, 681)
# dialog.__layout_fun_button.addWidget(dialog.button_takeoff)
# dialog.__layout_fun_button.addWidget(dialog.button_set_down)
#
# # self.__layout_fun_button.addWidget(self.label)
# dialog.__layout_main.addLayout(dialog.__layout_fun_button)
# dialog.__layout_main.addWidget(lab1)
#
# dialog.setLayout(dialog.__layout_main)
#self.show()
dialog.show()
#sys.exit(app.exec_())
dialog.exec()
def take_photo(self):
#video to photo
args = video_to_photo.parse_args()
if not os.path.exists(args.output):
os.makedirs(args.output)
print('Called with args:')
print(args)
video_to_photo.process_video(args.input, args.output, args.skip_frame)
#灰度值筛选
dir2 = r"C:\Users\xinluo\PycharmProjects\pythonProject\Camera Roll"
dir1 = r"C:\Users\xinluo\PycharmProjects\pythonProject\Saved Pictures"
names = os.listdir(dir1)
n = len(names)
print("文件数量", n)
res = 0
average_5 = 25565356
average_25 = 26409377
average_5_right = 10006019
# average_tmp = (average_25+average_5)//2
count = 0
# show(os.path.join(dir1, "uni4F6C.png"))
for i in range(n):
# 取图片
print(1234)
img = PIL.Image.open(os.path.join(dir1, names[i]))
file = os.path.join(dir1, names[i])
rmfile = os.path.join(dir2, names[i])
array = np.array(img)
num = array.sum(axis=0)
res_right = 0
for i in range(256, 512):
res_right += num[i]
average_5_right += res_right / n
#res_right > average_5_right
if (1):
print(4321)
shutil.copyfile(file, rmfile)
os.remove(file)
count += 1
print(average_5_right)
print(count)
#像素筛选
xiangsu_shuai.pixel()
#清晰度筛选
qingxi_shuai.read_path(r"C:\Users\xinluo\PycharmProjects\pythonProject\Camera Roll")
def button_open_camera_clicked(self):
if self.timer_camera.isActive() == False:
flag = self.cap.open(self.CAM_NUM)
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, 'warning', "请检查无人机是否连接正确", buttons=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera.start(30)
self.button_open_camera.setText('关闭摄像头')
else:
self.timer_camera.stop()
self.cap.release()
self.label_show_camera.clear()
self.button_open_camera.setText('打开摄像头')
def show_camera(self):
self.Drone = tello.Tello() # 创建飞行器对象
self.Drone.connect() # 连接到飞行器
self.Drone.streamon() # 开启视频传输
self.Drone.LOGGER.setLevel(logging.ERROR) # 只显示错误信息
#sleep(5) # 等待视频初始化
#kp.init() # 初始化按键处理模块
flag,OriginalImage = self.Drone.get_frame_read()
#flag, self.image = self.cap.read()
show = cv2.resize(OriginalImage, (640, 480))
show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0],
QtGui.QImage.Format_RGB888)
self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
#getKeyboardInput(drone=Drone, speed=70, image=Image) # 按键控制
cv2.imshow("Drone Control Centre", OriginalImage)
def showDialog(self):
dialog = QtWidgets.QDialog()
dialog.resize(1800, 1600)
button = QtWidgets.QPushButton('储存', dialog)
button.clicked.connect(dialog.close)
button.move(1350, 750)
dialog.setWindowTitle('建模视图')
dialog.setWindowModality(QtCore.Qt.ApplicationModal)
dialog.label = QtWidgets.QLabel(dialog)
dialog.label.setGeometry(QtCore.QRect(0, 0, 800, 600))
# 实例化自定义画布类
dialog.canvas = MyMatplotlibFigure(width=5, heigh=4, dpi=100)
#dialog.plotcos()
# t = np.arange(0.0, 5.0, 0.01)
# s = np.cos(2 * np.pi * t)
# dialog.canvas.mat_plot_drow_axes(t, s)
# dialog.canvas.figs.suptitle("sin") # 设置标题
# 重建3D点
dialog.canvas.mat_plot_drow_axes(Points3D[0], Points3D[1], Points3D[2])
dialog.canvas.figs.suptitle("SanDCJ") # 设置标题
dialog.hboxlayout = QtWidgets.QHBoxLayout(dialog.label)
dialog.hboxlayout.addWidget(dialog.canvas)
#不展示dialog只看figure
#dialog.exec()
def onClick_Button(self):
sender = self.sender() #获得button
#print(sender.text() + '按钮被按下')
app = QtWidgets.QApplication.instance()
#退出程序
app.quit()
if __name__ == '__main__':
inter_corner_shape = (9, 6)
size_per_grid = 0.023
# 储存用来标定相机的照片,这里是为畸变的照片
img_dir = ".\\pic\\RGB_camera_calib_img_1"
img_type = "jpg"
# 储存标定相机之后的参数,应该相机的内外参数与畸变参数
CameraParam_Path = '.\\CameraParam.txt'
# 要重建的目标图片
Images_Path = 'SubstitutionCalibration_Image_1/*.jpg'
# 计算相机参数
calib_RGB.calib(inter_corner_shape, size_per_grid, img_dir, img_type, CameraParam_Path)
# 读取相机参数
config = calib_RGB.readfromfile(CameraParam_Path)
K = np.array(config['mtx'])
# 计算3D点
Points3D = calib_RGB.epipolar_geometric(Images_Path, K)
app = QtWidgets.QApplication(sys.argv)
ui = Ui_MainWindow()
ui.show()
sys.exit(app.exec_())