Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Try to make a home ai with object recognition, making Javis at some point
- Just started coding like 3 days ago, I've been interested in machine learning for some time and I developed some code that might be interesting. But I don't know if the code is well written it's in python be warned.
- import sys
- import os
- import cv2
- import numpy as np
- import tensorflow as tf
- from PyQt5.QtWidgets import QApplication, QFileDialog, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
- from PyQt5.QtWidgets import QTextEdit
- from threading import Thread
- \# Define the model architecture
- model = tf.keras.Sequential()
- model.add(tf.keras.layers.Conv2D(32, kernel\_size=(3, 3), activation='relu', input\_shape=(1080, 1920, 3)))
- model.add(tf.keras.layers.MaxPooling2D(pool\_size=(2, 2)))
- model.add(tf.keras.layers.Conv2D(64, kernel\_size=(3, 3), activation='relu'))
- model.add(tf.keras.layers.MaxPooling2D(pool\_size=(2, 2)))
- model.add(tf.keras.layers.Flatten())
- model.add(tf.keras.layers.Dense(128, activation='relu'))
- model.add(tf.keras.layers.Dense(10, activation='softmax'))
- def create\_model(batch\_size):
- \# Define the model architecture
- model = tf.keras.Sequential()
- model.add(tf.keras.layers.Dense(64, input\_shape=(batch\_size, 32)))
- model.add(tf.keras.layers.Dense(64))
- model.add(tf.keras.layers.Dense(10))
- \# Compile the model
- model.compile(optimizer=tf.keras.optimizers.Adam(),
- loss=tf.keras.losses.SparseCategoricalCrossentropy(from\_logits=True),
- metrics=\['accuracy'\])
- return model
- \# Create the model with a batch size of 160
- model = create\_model(batch\_size=160)
- \# Compile the model
- model.compile(loss=tf.keras.losses.categorical\_crossentropy,
- optimizer=tf.keras.optimizers.Adam(),
- metrics=\['accuracy'\])
- class AppUI(QtWidgets.QWidget):
- def \_\_init\_\_(self):
- super().\_\_init\_\_()
- self.initUI()
- def initUI(self):
- \# Create a progress bar to display the predictions
- self.prediction\_bar = QtWidgets.QProgressBar(self)
- \# Set the style of the progress bar to match the "Ironman" aesthetic
- self.prediction\_bar.setStyleSheet("""
- QProgressBar {
- border: 2px solid #444;
- border-radius: 5px;
- background-color: #222;
- }
- QProgressBar::chunk {
- background-color: #ffa500;
- width: 10px;
- }
- """)
- \# Create a label for the video file input
- self.file\_label = QLabel('Video file:', self)
- \# Create a line edit for entering the video file path
- self.file\_edit = QLineEdit(self)
- \# Create a button for selecting the video file
- self.file\_button = QPushButton('Browse', self)
- self.file\_button.clicked.connect(self.browseFile)
- \# Create a label for the frame rate input
- self.fps\_label = QLabel('Frame rate:', self)
- \# Create a line edit for entering the frame rate
- self.fps\_edit = QLineEdit
- \# Add the progress bar to the layout
- layout = QtWidgets.QVBoxLayout(self)
- layout.addWidget(self.prediction\_bar)
- def updatePrediction(self, prediction):
- \# Set the value of the progress bar to the predicted value
- self.prediction\_bar.setValue(prediction)
- class AppUI(QWidget):
- def \_\_init\_\_(self):
- super().\_\_init\_\_()
- self.initUI()
- def initUI(self):
- \# Create a label for the video file input
- self.file\_label = QLabel('Video file:', self)
- \# Create a line edit for entering the video file path
- self.file\_edit = QLineEdit(self)
- \# Create a button for selecting the video file
- self.file\_button = QPushButton('Browse', self)
- self.file\_button.clicked.connect(self.browseFile)
- \# Create a label for the frame rate input
- self.fps\_label = QLabel('Frame rate:', self)
- \# Create a line edit for entering the frame rate
- self.fps\_edit = QLineEdit(self)
- \# Create a label for the resolution input
- self.resolution\_label = QLabel('Resolution:', self)
- \# Create a line edit for entering the resolution
- self.resolution\_edit = QLineEdit(self)
- \# Create a start button
- self.start\_button = QPushButton('Start', self)
- self.start\_button.clicked.connect(self.startRecognition)
- \# Create a stop button
- self.stop\_button = QPushButton('Stop', self)
- self.stop\_button.clicked.connect(self.stopRecognition)
- \# Create a text edit for displaying the predictions
- self.predictions\_edit = QTextEdit(self)
- \# Create a layout and add the UI elements to it
- layout = QVBoxLayout(self)
- layout.addWidget(self.file\_label)
- layout.addWidget(self.file\_edit)
- layout.addWidget(self.file\_button)
- layout.addWidget(self.fps\_label)
- layout.addWidget(self.fps\_edit)
- layout.addWidget(self.resolution\_label)
- layout.addWidget(self.resolution\_edit)
- layout.addWidget(self.start\_button)
- layout.addWidget(self.stop\_button)
- layout.addWidget(self.predictions\_edit)
- def browseFile(self):
- \# Open a file dialog to select the video file
- options = QFileDialog.Options()
- options |= QFileDialog.ReadOnly
- file\_name, \_ = QFileDialog.getOpenFileName(self, 'Select Video File', '',
- 'Video Files (\*.mp4 \*.avi);;All Files (\*)', options=options)
- \# Update the line edit with the selected file path
- self.file\_edit.setText(file\_name)
- def preprocess\_frames(frames):
- \# Resize the frames to the desired resolution
- resized\_frames = \[\]
- for frame in frames:
- resized\_frame = cv2.resize(255, (1920, 1080))
- resized\_frames.append(resized\_frame)
- \# Convert the frames to tensorflow's preferred format
- processed\_frames = tf.convert\_to\_tensor(resized\_frames, dtype=tf.float32)
- \# Normalize the pixel values
- normalized\_frames = processed\_frames / 255.0
- return normalized\_frames
- def preprocess\_input\_data(frames, fps, resolution):
- \# Preprocess the frames
- preprocessed\_frames = preprocess\_frames(frames, fps, resolution)
- \# Add an additional dimension for the batch size
- preprocessed\_frames = np.expand\_dims(preprocessed\_frames, axis=4)
- return preprocessed\_frames
- def startRecognition(self):
- \# Start the recognition process in a separate thread
- self.recognition\_thread = Thread(target=self.run\_recognition)
- self.recognition\_thread.start()
- def stopRecognition(self):
- \# Stop the recognition process by interrupting the thread
- self.recognition\_thread.interrupt()
- def run\_recognition(self):
- \# Get the video file path from the line edit
- file\_path = self.file\_edit.text()
- \# Open the video file
- video = cv2.VideoCapture(file\_path)
- \# Get the frame rate and resolution from the line edits
- fps = int(self.fps\_edit.text())
- width, height = map(int, self.resolution\_edit.text().split('x'))
- resolution = (width, height)
- \# Read the frames from the video
- success, frame = video.read()
- frames = \[\]
- while success:
- \# Save the frame
- frames.append(frame)
- \# Read the next frame
- success, frame = video.read()
- \# Close the video file
- video.release()
- \# Preprocess the input data to match the model's input shape
- input\_data = preprocess\_input\_data(frames, fps, resolution)
- \# Load the tensorflow model
- model = tf.keras.models.load\_model('model.h5')
- \# Generate predictions using the model
- predictions = model.predict(input\_data)
- \# Display the predictions in the text edit
- self.predictions\_edit.setPlainText('\\n'.join(predictions))
- if \_\_name\_\_ == '\_\_main\_\_':
- \# Create a Qt application
- app = QApplication(sys.argv)
- \# Create an instance of the AppUI class
- ui = AppUI()
- \# Show the UI
- ui.show()
- \# Run the Qt application
- sys.exit(app.exec\_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement