#Importing necessary libraries, mainly the OpenCV, and PyQt libraries
import cv2
import numpy as np
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from cv2 import *

class ShowVideo(QtCore.QObject):

	camera_port = 0
	camera = cv2.VideoCapture(camera_port)
	VideoSignal = QtCore.pyqtSignal(QtGui.QImage)

	def __init__(self, parent = None):
		super(ShowVideo, self).__init__(parent)

	@QtCore.pyqtSlot()
	def startVideo(self):

		run_video = True
		while run_video:
			ret, image = self.camera.read()
			
			color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

			height, width, _ = color_swapped_image.shape
			
			qt_image = QtGui.QImage(color_swapped_image.data,
									width, 
									height,
			  						color_swapped_image.strides[0],
			   						QtGui.QImage.Format_RGB888)

			pixmap = QtGui.QPixmap(qt_image)
			# qt_image = pixmap.scaled(750, 600, QtCore.Qt.KeepAspectRatio)
			# qt_image = QtGui.QImage(qt_image)
			qt_image = QtGui.QImage(pixmap)

			self.VideoSignal.emit(qt_image)

	def startVideo2(self):
    
		run_video = True
		while run_video:
			ret, image = self.camera.read()
			
			color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

			b, g, r = cv2.split(color_swapped_image)
			b_equal = equalizeHist(b)
			g_equal = equalizeHist(g)
			r_equal = equalizeHist(r)
			result = merge([b_equal, g_equal, r_equal])

			height, width, _ = color_swapped_image.shape

			qt_image2 = QtGui.QImage(result,
									width, 
									height,
									color_swapped_image.strides[0],
									QtGui.QImage.Format_RGB888)

			pixmap2 = QtGui.QPixmap(qt_image2)
			# qt_image2 = pixmap2.scaled(750, 600, QtCore.Qt.KeepAspectRatio)
			# qt_image2 = QtGui.QImage(qt_image2)
			qt_image2 = QtGui.QImage(pixmap2)

			self.VideoSignal.emit(qt_image2)



class ImageViewer(QtWidgets.QWidget):
	def __init__(self, parent = None):
		super(ImageViewer, self).__init__(parent)
		self.image = QtGui.QImage()
		self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)

	def paintEvent(self, event):
		painter = QtGui.QPainter(self)
		painter.drawImage(0,0, self.image)
		self.image = QtGui.QImage()

	def initUI(self):
		self.setWindowTitle('Test')

	
	@QtCore.pyqtSlot(QtGui.QImage)
	def setImage(self, image):
		if image.isNull():
			print("viewer dropped frame!")

		self.image = image
		if image.size() != self.size():
			self.setFixedSize(image.size())
		self.update()


if __name__ == '__main__':

	app = QtWidgets.QApplication(sys.argv)
	thread = QtCore.QThread()
	thread.start()
	thread2 = QtCore.QThread()
	thread2.start()

	vid = ShowVideo()
	vid.moveToThread(thread)
	vid2 = ShowVideo()
	vid2.moveToThread(thread2)
	image_viewer = ImageViewer()
	image_viewer2 = ImageViewer()

	vid.VideoSignal.connect(image_viewer.setImage)
	vid2.VideoSignal.connect(image_viewer2.setImage)

	push_button = QtWidgets.QPushButton('Original')
	push_button.clicked.connect(vid.startVideo)
	push_button2 = QtWidgets.QPushButton('Restored')
	push_button2.clicked.connect(vid2.startVideo2)
	vertical_layout = QtWidgets.QHBoxLayout()

	vertical_layout.addWidget(image_viewer)
	vertical_layout.addWidget(image_viewer2)
	vertical_layout.addWidget(push_button)
	vertical_layout.addWidget(push_button2)

	layout_widget = QtWidgets.QWidget()
	layout_widget.setLayout(vertical_layout)

	main_window = QtWidgets.QMainWindow()
	main_window.setCentralWidget(layout_widget)
	main_window.resize(1000,800)
	main_window.show()
	sys.exit(app.exec_())