如何实现python在虚拟环境GTA中自动驾驶程序的重启restart?
问题描述:
求救???? 大神们好, 想请教一个问题,我现在和同学一起在写一个python项目, 是在GTA里实现自动驾驶汽车.代码中在 def button() 里面的 x.start() 会启动整个程序,我们还需要一个重启功能restart。 现在不知道怎么restart x.start()。
就是我们的这个程序有按钮 button 可以点一下 开始键 结束键 现在需要弄好重新启动键。
有一个思路是重新调用一下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(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()
答
重启restart做2步操作;
1.调用结束键对应的功能停止;
2.调用start函数功能启动。
答
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了,您看下是否解决问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632