2017-03-08 3 views
-1

Ich habe versucht, die GUI (tkinter) in mein Skript hinzuzufügen, aber ohne Erfolg. Wenn mir jemand helfen kann, wäre ich so dankbar. Ich benutze Python 3.6 und ich denke, die neueste opencv?Hinzufügen von GUI zu den Python-Skripten

Ich habe erst vor 2 Wochen mit der Programmierung begonnen. Also, irgendwie neu in all dem. Im Grunde möchte ich ein Fenster erstellen, das nur das Bild aus meinem Ordner auswählt und dann durch das Skript verarbeitet, so dass ich das Skript nicht ändern muss, wenn ich ein anderes Bild verwenden möchte. Ich hoffe, das macht Sinn.

Dies ist das Skript, das ich von Chris Dahms von Youtube genommen habe, und es geschafft habe, es zu ändern, was ich will.

import cv2 
import numpy as np 
import os 

import DetectChars 
import DetectPlates 
import PossiblePlate 

SCALAR_BLACK = (0.0, 0.0, 0.0) 
SCALAR_WHITE = (255.0, 255.0, 255.0) 
SCALAR_YELLOW = (0.0, 255.0, 255.0) 
SCALAR_GREEN = (0.0, 255.0, 0.0) 
SCALAR_CYAN = (255.0, 255.0, 0.0) 

showSteps = False 

def main(): 

blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN()   

if blnKNNTrainingSuccessful == False:        
    print ("\nerror: KNN training was not successful\n")  
    return               

imgOriginalScene = cv2.imread("CAR/Malaysia/22.jpg") 

if imgOriginalScene is None: 
    print ("\nerror: image not read from file \n\n") 
    os.system("pause") 
    return 

if imgOriginalScene is None:        
    print ("\nerror: image not read from file \n\n")  
    os.system("pause")         
    return 

listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene)   

listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates)   

cv2.imshow("imgOriginalScene", imgOriginalScene)    

if len(listOfPossiblePlates) == 0:       
    print ("\nno license plates were detected\n")    
else:             



    listOfPossiblePlates.sort(key = lambda possiblePlate: len(possiblePlate.strChars), reverse = True) 


    licPlate = listOfPossiblePlates[0] 

    cv2.imshow("Image Plate", licPlate.imgPlate)   
    cv2.imshow("Image Threshold", licPlate.imgThresh) 

    if len(licPlate.strChars) == 0:      
     print ("\nno characters were detected\n\n")  
     return          


    drawRedRectangleAroundPlate(imgOriginalScene, licPlate)   

    print ("\nlicense plate read from image = " + licPlate.strChars + "\n")  
    print ("----------------------------------------") 

    writeLicensePlateCharsOnImage(imgOriginalScene, licPlate)  

    cv2.imshow("imgOriginalScene", imgOriginalScene)    

    cv2.imwrite("imgOriginalScene.png", imgOriginalScene)   



cv2.waitKey(0)    

return 

def drawRedRectangleAroundPlate(imgOriginalScene, licPlate): 

p2fRectPoints = cv2.boxPoints(licPlate.rrLocationOfPlateInScene)   

cv2.line(imgOriginalScene, tuple(p2fRectPoints[0]), tuple(p2fRectPoints[1]), SCALAR_RED, 2)  
cv2.line(imgOriginalScene, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), SCALAR_RED, 2) 
cv2.line(imgOriginalScene, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), SCALAR_RED, 2) 
cv2.line(imgOriginalScene, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), SCALAR_RED, 2) 


def writeLicensePlateCharsOnImage(imgOriginalScene, licPlate): 
ptCenterOfTextAreaX = 0       
ptCenterOfTextAreaY = 0 

ptLowerLeftTextOriginX = 0       
ptLowerLeftTextOriginY = 0 

sceneHeight, sceneWidth, sceneNumChannels = imgOriginalScene.shape 
plateHeight, plateWidth, plateNumChannels = licPlate.imgPlate.shape 

intFontFace = cv2.FONT_HERSHEY_SIMPLEX     
fltFontScale = float(plateHeight)/30.0      
intFontThickness = int(round(fltFontScale * 2))   

textSize, baseline = cv2.getTextSize(licPlate.strChars, intFontFace, fltFontScale, intFontThickness)  


((intPlateCenterX, intPlateCenterY), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg) = licPlate.rrLocationOfPlateInScene 

intPlateCenterX = int(intPlateCenterX)   
intPlateCenterY = int(intPlateCenterY) 

ptCenterOfTextAreaX = int(intPlateCenterX)   

if intPlateCenterY < (sceneHeight * 0.75):            
    ptCenterOfTextAreaY = int(round(intPlateCenterY)) + int(round(plateHeight * 1.6)) 
else:                     
    ptCenterOfTextAreaY = int(round(intPlateCenterY)) - int(round(plateHeight * 1.6))  


textSizeWidth, textSizeHeight = textSize    

ptLowerLeftTextOriginX = int(ptCenterOfTextAreaX - (textSizeWidth/2))   
ptLowerLeftTextOriginY = int(ptCenterOfTextAreaY + (textSizeHeight/2))   


cv2.putText(imgOriginalScene, licPlate.strChars, (ptLowerLeftTextOriginX, ptLowerLeftTextOriginY), intFontFace, fltFontScale, SCALAR_CYAN, intFontThickness) 



if __name__ == "__main__": 
main() 

cv2.waitKey() 
cv2.destroyAllWindows() 

Vorverarbeitungsstufe

# Preprocess.py 

import numpy as np 
import math 

# module level variables ########################################################################## 
GAUSSIAN_SMOOTH_FILTER_SIZE = (5, 5) 
ADAPTIVE_THRESH_BLOCK_SIZE = 19 
ADAPTIVE_THRESH_WEIGHT = 9 


def preprocess(imgOriginal): 
imgGrayscale = extractValue(imgOriginal) 

imgMaxContrastGrayscale = maximizeContrast(imgGrayscale) 

height, width = imgGrayscale.shape 

grayscaled = cv2.cvtColor(imgOriginal,cv2.COLOR_BGR2GRAY) 

imgBlurred = np.zeros((height, width, 1), np.uint8) 

imgBlurred, otsu = cv2.threshold(grayscaled,125,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 

imgThresh = cv2.medianBlur(otsu,5) 

return imgGrayscale, imgThresh 
# end function 


def extractValue(imgOriginal): 
height, width, numChannels = imgOriginal.shape 

imgHSV = np.zeros((height, width, 3), np.uint8) 

imgHSV = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2HSV) 

imgHue, imgSaturation, imgValue = cv2.split(imgHSV) 

return imgValue 
# end function 

def maximizeContrast(imgGrayscale): 

height, width = imgGrayscale.shape 

imgTopHat = np.zeros((height, width, 1), np.uint8) 
imgBlackHat = np.zeros((height, width, 1), np.uint8) 

structuringElement = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) 

imgTopHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_TOPHAT, structuringElement) 
imgBlackHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_BLACKHAT, structuringElement) 

imgGrayscalePlusTopHat = cv2.add(imgGrayscale, imgTopHat) 
imgGrayscalePlusTopHatMinusBlackHat = cv2.subtract(imgGrayscalePlusTopHat, imgBlackHat) 

return imgGrayscalePlusTopHatMinusBlackHat 
# end function 
+0

Versuchen und genauer gesagt über das, was Sie suchen und sind nur Code der direkt zu Ihrer Frage. Sie erhalten mehr nützliche Antworten, da die Leute wissen, wofür Sie Hilfe benötigen. – darrenvba

+0

meine schlechte, das ist das erste Mal, dass ich versuche, Frage hier zu stellen .. Ich möchte GUI zum Skript hinzufügen. Ich möchte ein Fenster erstellen, das nur das Bild aus meinem Ordner auswählt und dann über das Skript verarbeitet, so dass ich das Skript nicht ändern muss, wenn ich ein anderes Bild verwenden möchte. Ich hoffe, dass Sinn machen .. Ich werde die Beschreibung erneut bearbeiten .. danke für die Antwort. –

Antwort

0

Wenn alles, was Sie wollen, sind ein Fenster, eine Datei dann sollte diese Arbeit wählen.

import Tkinter 
from Tkinter import * 
import tkSimpleDialog 
from tkFileDialog import askopenfilename 

master = Tk() 
master.withdraw() 
my_file = askopenfilename() 
mainloop() 
+1

danke, es funktioniert..aber das für Python 3, gerade geändert zu tkinter, der dritte Zeile geändert von tkinter Import Simpledialog und die vierte Zeile geändert von tkinter.filedialog Import askopenfilename und es sollte laufen .. vielen Dank noch einmal für die Hilfe –

0

i Gtk3 für Ihre GUI empfehlen. hier ist ein einfaches GTK Fenster mit Taste:

#!/usr/bin/env python3 

import gi 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 

class Window(Gtk.Window): 
    def __init__(self): 
     Gtk.Window.__init__(self) 
     self.connect('destroy', lambda q: Gtk.main_quit()) 

     button = Gtk.Button("Gtk.Button") 
     button.connect("clicked", self.on_button_clicked) 

     grid = Gtk.Grid() 
     grid.attach(button, 0, 0, 1, 1) 

     self.add(grid) 
     self.show_all() 

    def on_button_clicked(self, button): 
     print("Gtk.Button was clicked") 

w = Window() 
Gtk.main() 
+0

vielen Dank für die Antwort. Ich werde versuchen, das in meinem Projekt zu verwenden. und hier reiße ich Kollege und meine Haare, weil ich nicht genug Fähigkeit hatte, Sachen zu erledigen .. danke Ihnen so sehr wieder .. –

Verwandte Themen