Jun-10-2021, 09:33 AM
Hi
im new to python so of course i do understand that if this is a stupid question or at least an easy fix. my code does work as it takes a picture, records a video when reading something from the serial port. the issue i am facing is that once it read something it should take a picture and record a video, while it is doing that for some reason it still continues to read, so as soon as the picture is taken and the video is recorded for 8 seconds it just starts doing it again, taking another picture and making another video. im guessing that the code is storing the data from the serial port so as soon as it is done it does it again. so is there a way to disable the port and then re-enable it once the video is done.
below is my code
Thank you
im new to python so of course i do understand that if this is a stupid question or at least an easy fix. my code does work as it takes a picture, records a video when reading something from the serial port. the issue i am facing is that once it read something it should take a picture and record a video, while it is doing that for some reason it still continues to read, so as soon as the picture is taken and the video is recorded for 8 seconds it just starts doing it again, taking another picture and making another video. im guessing that the code is storing the data from the serial port so as soon as it is done it does it again. so is there a way to disable the port and then re-enable it once the video is done.
below is my code
# Write your code here :-)
import cv2
import numpy as np
import tkinter as tk
from PIL import Image, ImageTk
import datetime
import os
import argparse
import serial
from picamera import PiCamera
import time
serialPort = serial.Serial(port = "/dev/ttyUSB0", baudrate=115200,
bytesize=8, timeout=0, stopbits=serial.STOPBITS_ONE)
output_path = "/home/pi/Pictures"
window = tk.Tk()
window.wm_title("Trusign Camera")
window.config(background="#FFFFFF")
var = tk.StringVar(window)
imageFrame = tk.Frame(window, width=1000, height=840)
imageFrame.grid(row=0, column=0, padx=0, pady=0)
lmain = tk.Label(imageFrame)
lmain.grid(row=0, column=0)
cap = cv2.VideoCapture(0)
#cap.set(3, 1000)
#cap.set(4, 840)
cap.set(3, 640)
cap.set(4, 480)
global check
check = 0
def show_frame():
_, frame = cap.read()
#frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
show_frame.curent_frame = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, show_frame)
serial_read_data()
def serial_read_data():
global check
if(serialPort.in_waiting > 0 and check == 0):
serialString = serialPort.readline()
global serialresult
global serialresulting
serialresult = int(serialString.decode('Ascii'))
serialresulting = str(serialresult )
var.set(serialresulting + "MPH")
serialPort.flush()
if(serialresult > 18 and check == 0):
check = 1
print("Take SnapShot")
serial_read_data.speed_result = serialresult
take_snapshot()
else:
pass
else:
pass
def take_snapshot():
ts = datetime.datetime.now() # grab the current timestamp
filename = "{}.png".format(ts.strftime("%Y-%m-%d_%H-%M-%S")) # construct filename
p = os.path.join(output_path, filename) # construct output path
speed_result_2 = serial_read_data.speed_result
image2 = np.array(show_frame.curent_frame)
cv2.putText(image2,"Speed :" + str(speed_result_2)+"Mph", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
cv2.imwrite(p,cv2.cvtColor(image2, cv2.COLOR_RGB2BGR))
print(p)
take_video()
def take_video():
global check
td = datetime.datetime.now()
filename2 = "{}.mp4".format(td.strftime("%Y-%m-%d_%H-%M-%S"))
n = os.path.join(output_path, filename2)
save_name = "output.mp4"
fps = 20
width = 600
height = 480
output_size = (width, height)
out = cv2.VideoWriter(n,cv2.VideoWriter_fourcc('M','J','P','G'), fps, output_size )
for z in range(0,161,1):
_, frame2 = cap.read()
#cv2.putText(frame2,"Speed : 0Mph", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
out.write(cv2.resize(frame2, output_size))
if(z == 160):
out.release()
print("Save")
check = 0
else:
print("")
#Slider window (slider controls stage position)
sliderFrame = tk.Frame(window, width=200, height=480)
sliderFrame.grid(row = 0, column=640, padx=0, pady=0)
sliderFrame.config(background="#FFFFFF")
l = tk.Label(sliderFrame, text="Speed")
l.config(font =("Courier", 23), background="#FFFFFF")
l2 = tk.Label(sliderFrame, textvariable = var)
l2.config(font =("Courier", 15), background="#FFFFFF")
btn = tk.Button(sliderFrame, text = "Test Shot", command=take_snapshot)
l.place(x=40, y=0)
l2.place(x=70, y=50)
btn.place(x=60, y=440)
#serial_read_data()
show_frame()
window.mainloop()i have been stuck on it for ages and if anyone could help that would be great Thank you
