大神们好, 想请教一个问题,我现在和同学一起在写一个python项目, 是在GTA里实现自动驾驶汽车.代码中在 def button() 里面的 x.start() 会启动整个程序,我们还需要一个重启功能restart。 现在不知道怎么restart x.start()。 麻烦指点一下!感激不尽!
import numpy as np
from PIL import Image
import cv2
import time
import glob
from movement import forward, forwardleft, forwardright
from mss import mss
from Draw_lanes import Draw_lanes
from numpy import ones,vstack
from numpy.linalg import lstsq
from statistics import mean
Should_run=True
def Roi(image, vertices):
#blank mask:
mask = np.zeros_like(image)
# fill the mask
cv2.fillPoly(mask, vertices, 255)
# now only show the area that is the mask
masked = cv2.bitwise_and(image, mask)
return masked
def Screen_Capture(top, left, width, height):
monitor = {"top": top, "left": left, "width": width, "height": height}
sct = mss()
while(True):
while(Should_run):
last_time = time.time()
img=sct.grab(monitor)
screen= np.array(img)
last_time = time.time()
ogim,newim, m1,m2 =Process_img(screen)
cv2.imshow("orginal image", ogim)
cv2.imshow("prossed image", newim)
if m1 < 0 and m2 < 0:
forwardright()
print("right")
elif m1 > 0 and m2 > 0:
forwardleft()
print("left")
else:
forward()
print("straight")
if cv2.waitKey(25) & 0xFF == ord('x'):
cv2.destroyAllWindows()
break
def Process_img(image):
m1 =0
m2 =0
original_image = image
# convert to gray
processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# edge detection
processed_img = cv2.Canny(processed_img, threshold1 = 200, threshold2=300)
region_of_interest_verticies= np.array([[200,450],[200,450],[300,200],[500,200],[600,450],[600,450],], np.int32)
processed_img = cv2.GaussianBlur(processed_img,(5,5),0)
processed_img= Roi(processed_img, [region_of_interest_verticies])
lines= cv2.HoughLinesP(processed_img, 1, np.pi/180, 180, 20, 15)
try:
l1, l2, m1, m2 = Draw_lanes(original_image,lines)
original_image=cv2.line(original_image, (l1[0], l1[1]), (l1[2], l1[3]), [0,255,255], 30)
original_image=cv2.line(original_image, (l2[0], l2[1]), (l2[2], l2[3]), [0,255,255], 30)
except Exception as e:
print(str(e))
pass
return original_image,processed_img,m1, m2
from tkinter import *
from tkinter import ttk
import threading
import time
started = False
def initialize():
global a
a=0
def self_drive():
a=1
global Should_run
Should_run = True
Screen_Capture(40,60,800,600)
x=threading.Thread(target=self_drive)
def self_drive_stop():
global Should_run
Should_run = False
a=2
def button1():
if x.is_alive():
x.run()
if started==False:
print("I made it here ")
x.start()
def inter():
root = Tk()
root.title("pyBros")
root.minsize(width=200,height=100)
button=Button(root, text="Self Drive Start",width=20,command=button1)
stop= Button(root,text="Stop Drive",width=20,command=self_drive_stop)
manual_drive=Button(root,text="manual Drive",width=20)
button.pack(side=LEFT)
stop.pack(side=LEFT)
manual_drive.pack(side=LEFT)
mainloop()