Not able Load to keras model in QThread in PyQt5 GUI

0

I am using keras model and trying to load it in PyQT5 GUI using QThread but GUI Window stops working when code execution come to load model and shows error "Process finished with exit code -1073740791 (0xC0000409)"

I have tried without QThread and code running correctly but GUI Screen Shows message like Not Responding and then try to QThread but code stop working when come to load model line

I have also work with QMutex still issue not resolve.

In Code I write QThread with SetModel,predict, show_detected_objects, and call all this method to run

Main Window has pushbotton and connect to Detection method as like self.DetectionPushButton.clicked.connect(self.Detection)

In Detection Method I called Threading class and run method.

When I clicked on Pushbutton code run good till self.modelpath and then GUI crash with error mention above.

Please Guide me the Below code is correctly implement or not??

from PyQt5 import QtCore as qtc
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
import os.path
import numpy as np
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import os
import cv2
from PIL import Image
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
from keras_retinanet.utils.visualization import draw_box, draw_caption
from keras_retinanet.utils.colors import label_color
from keras_retinanet import models
import keras_retinanet

RANDOM_SEED = 42
np.random.seed(RANDOM_SEED)
tf.random.set_seed(RANDOM_SEED)

class Threading(qtc.QThread):

    def __init__(self, imagepath, csvpath):
        super(self.__class__, self).__init__()
        self.imagepath = imagepath
        self.csvpath = csvpath
        self.THRES_SCORE = 0.70  # rediction Accuracy
        self._mutex = qtc.QMutex()
        self.CWD_PATH = os.getcwd()
        self.MODEL_FOLDER = os.path.join(self.CWD_PATH, "Model_Data")
        self.labels_to_names = pd.read_csv(self.CLASSES_FILE, header=None).T.loc[0].to_dict()
        self.RAW_CSV_PATH = os.path.join(self.INPUT_DATA_FOLDER, 'raw_input_data.csv')

    def __del__(self):
        self.wait()

    def SetModel(self):
        self.model_path = os.path.join(self.MODEL_FOLDER, 'resnet50_csv_02.h5')
        self._mutex.lock()
        self.model50 = keras_retinanet.models.load_model(self.model_path, backbone_name='resnet50')
        self._mutex.unlock()
        self.model50 = models.convert_model(self.model50, anchor_params=self.anchor_parameters)
        self._mutex.unlock()
        return self.model50

    def predict(self,image, model):
        image = preprocess_image(image.copy())
        image, scale = resize_image(image)
        boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))
        boxes /= scale
        return boxes, scores, labels

    def show_detected_objects(self,model):
        temp_df = pd.read_csv(self.RAW_CSV_PATH)
        Detection_Count = 1
        for i in range(len(temp_df.index)):
            df_row = temp_df.iloc[i]
            file = df_row.Image_Name
            img_path = os.path.join(self.imagepath, file)
            image = read_image_bgr(img_path)
            boxes, scores, labels = self.predict(image, model)
            draw = image.copy()
            draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
            det = False
            for box, score, label in zip(boxes[0], scores[0], labels[0]):
                if score < self.THRES_SCORE:
                    break
                det = True
                color = label_color(label)
                b = box.astype(int)
                draw_box(draw, b, color=color)
                caption = "{} {:.3f}".format(self.labels_to_names[label], score)
                draw_caption(draw, b, caption)
                Detection_Count += 1
            if det == True:
                draw = Image.fromarray(draw)
                draw.save(os.path.join(self.IMG_OUTPUT_FOLDER, file))

    def run(self):
        if self.imagepath != "" and self.csvpath != "" :
            self.model1 = self.SetModel()
            self.show_detected_objects(self.model1)

class MainWindow(qtw.QMainWindow):
    def __init__(self):
        super().__init__()

        self.csvpath = ""
        self.imagepath = ""
        self.Time_Interval = 3 # Time Interval in second for Slicing from video
        self.createUI1()

    def createUI1(self):
        central_widget_Third_Tab = qtw.QHBoxLayout()
        self.disp = qtw.QVBoxLayout()
        self.disp.setAlignment(qtc.Qt.AlignCenter)
        self.imageLayout = qtw.QHBoxLayout()

        self.CSVLayout = qtw.QHBoxLayout()
        self.myTextBoxImage = qtw.QLineEdit(self)
        self.myTextBoxCSV = qtw.QLineEdit(self)

        self.Image_Push_Button = qtw.QPushButton("Browse Image Folder")
        self.Image_Push_Button.clicked.connect(self.openImage)

        self.CSV_Push_Button = qtw.QPushButton("Browse CSV File")
        self.CSV_Push_Button.clicked.connect(self.openCSV)

        self.DetectionPushButton = qtw.QPushButton("Start Processing")
        self.DetectionPushButton.clicked.connect(self.Detection)

        self.videoLayout.addWidget(self.myTextBoxImage)
        self.videoLayout.addWidget(self.Image_Push_Button)

        self.CSVLayout.addWidget(self.myTextBoxCSV)
        self.CSVLayout.addWidget(self.CSV_Push_Button)

        self.disp.addLayout(self.imageLayout)
        self.disp.addLayout(self.CSVLayout)
        self.disp.addWidget(self.DetectionPushButton)

        central_widget_Third_Tab.addLayout(self.disp)
        self.setLayout(central_widget_Third_Tab)
        self.show()
                              #filter="All Files(*.*);;Text Files(*.txt)"
    def openImage(self):
        fileNameraw = qtw.QFileDialog.getOpenFileName(self, "Open File", os.path.expanduser('~'))
        self.videopath = fileNameraw[0]
        self.myTextBoxImage.setText(self.imagepath)

    def openCSV(self):
        fileNameraw = qtw.QFileDialog.getOpenFileName(self, "Open File", os.path.expanduser('~'),filter="CSV File(*.csv)")
        self.csvpath = fileNameraw[0]
        self.myTextBoxCSV.setText(self.csvpath)

    def Detection(self):
        self.my_thread = Threading(csvpath=self.csvpath,imagepath=self.imagepath)
        self.my_thread.start()



python
keras
pyqt
pyqt5
qthread
asked on Stack Overflow Jan 2, 2020 by Milan Soliya • edited Jan 2, 2020 by Milan Soliya

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0