littleprince59 2021-04-27 15:38 采纳率: 0%
浏览 26

如何实现python模拟自动驾驶程序的重启restart?求助!!!

大神们好, 想请教一个问题,我现在和同学一起在写一个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()
  • 写回答

2条回答 默认 最新

  • littleprince59 2021-04-27 15:48
    关注

    如果有表述不清楚, 麻烦告知!谢谢!

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧