PySimpleGUI/DemoPrograms/Demo_OpenCV_Simple_GUI.py

77 lines
3.2 KiB
Python
Raw Permalink Normal View History

import PySimpleGUI as sg
import cv2
import numpy as np
"""
Demo program that displays a webcam using OpenCV and applies some very basic image functions
- functions from top to bottom -
none: no processing
threshold: simple b/w-threshold on the luma channel, slider sets the threshold value
canny: edge finding with canny, sliders set the two threshold values for the function => edge sensitivity
blur: simple Gaussian blur, slider sets the sigma, i.e. the amount of blur smear
hue: moves the image hue values by the amount selected on the slider
enhance: applies local contrast enhancement on the luma channel to make the image fancier - slider controls fanciness.
"""
2018-10-29 00:01:03 +00:00
def main():
2019-12-24 23:52:47 +00:00
sg.theme('LightGreen')
2018-10-29 00:01:03 +00:00
# define the window layout
layout = [
2020-05-07 09:49:44 +00:00
[sg.Text('OpenCV Demo', size=(60, 1), justification='center')],
[sg.Image(filename='', key='-IMAGE-')],
[sg.Radio('None', 'Radio', True, size=(10, 1))],
2020-05-07 09:49:44 +00:00
[sg.Radio('threshold', 'Radio', size=(10, 1), key='-THRESH-'),
sg.Slider((0, 255), 128, 1, orientation='h', size=(40, 15), key='-THRESH SLIDER-')],
[sg.Radio('canny', 'Radio', size=(10, 1), key='-CANNY-'),
sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='-CANNY SLIDER A-'),
sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='-CANNY SLIDER B-')],
[sg.Radio('blur', 'Radio', size=(10, 1), key='-BLUR-'),
sg.Slider((1, 11), 1, 1, orientation='h', size=(40, 15), key='-BLUR SLIDER-')],
[sg.Radio('hue', 'Radio', size=(10, 1), key='-HUE-'),
sg.Slider((0, 225), 0, 1, orientation='h', size=(40, 15), key='-HUE SLIDER-')],
[sg.Radio('enhance', 'Radio', size=(10, 1), key='-ENHANCE-'),
sg.Slider((1, 255), 128, 1, orientation='h', size=(40, 15), key='-ENHANCE SLIDER-')],
[sg.Button('Exit', size=(10, 1))]
]
2018-10-29 00:01:03 +00:00
# create the window and show it without the plot
2020-05-07 09:49:44 +00:00
window = sg.Window('OpenCV Integration', layout, location=(800, 400))
2018-10-29 00:01:03 +00:00
cap = cv2.VideoCapture(0)
2020-05-07 09:49:44 +00:00
2018-10-29 00:01:03 +00:00
while True:
2020-05-07 09:49:44 +00:00
event, values = window.read(timeout=20)
if event == 'Exit' or event == sg.WIN_CLOSED:
break
2018-10-29 00:01:03 +00:00
ret, frame = cap.read()
2020-05-07 09:49:44 +00:00
if values['-THRESH-']:
2018-10-29 00:01:03 +00:00
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)[:, :, 0]
2020-05-07 09:49:44 +00:00
frame = cv2.threshold(frame, values['-THRESH SLIDER-'], 255, cv2.THRESH_BINARY)[1]
elif values['-CANNY-']:
frame = cv2.Canny(frame, values['-CANNY SLIDER A-'], values['-CANNY SLIDER B-'])
elif values['-BLUR-']:
frame = cv2.GaussianBlur(frame, (21, 21), values['-BLUR SLIDER-'])
elif values['-HUE-']:
2018-10-29 00:01:03 +00:00
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
2020-05-07 09:49:44 +00:00
frame[:, :, 0] += int(values['-HUE SLIDER-'])
2018-10-29 00:01:03 +00:00
frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
2020-05-07 09:49:44 +00:00
elif values['-ENHANCE-']:
enh_val = values['-ENHANCE SLIDER-'] / 40
2018-10-29 00:01:03 +00:00
clahe = cv2.createCLAHE(clipLimit=enh_val, tileGridSize=(8, 8))
lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
lab[:, :, 0] = clahe.apply(lab[:, :, 0])
frame = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
imgbytes = cv2.imencode('.png', frame)[1].tobytes()
2020-05-07 09:49:44 +00:00
window['-IMAGE-'].update(data=imgbytes)
window.close()
main()