-
Notifications
You must be signed in to change notification settings - Fork 4
/
gtasapy_new.py
110 lines (87 loc) · 3.09 KB
/
gtasapy_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image, ImageGrab
import cv2
import time
from directKeys import PressKey, ReleaseKey, W, A, S, D, J
# import pyHook, pythoncom
#########################################################
######################### START #########################
#########################################################
from helper_functions import draw_lines, length_of_line, process_img, roi, straight, left, right, slow_ya_roll
from lane_detection import draw_lanes
from coco_object_detection import ObjectDetection
real_height = 420.0 # experimental value
focal_length = 10.0 # experimental value
brain_ON = False
objectDetection = ObjectDetection()
objectDetection.initialize()
# Counter of 4 seconds before starting.
for i in list(range(4))[::-1]:
print(i+1)
time.sleep(1)
time_difference = time.time()
while True:
# FOR GTA-SA
game_screen = np.asarray(ImageGrab.grab(bbox=(1, 35, 640, 420)))
# FOR GTAV
# game_screen = np.asarray(ImageGrab.grab(bbox=(1, 35, 800, 600)))
game_screen = cv2.cvtColor(game_screen, cv2.COLOR_BGR2RGB)
# image_np = game_screen
boxes, scores, classes, num_detections, image_with_detections = objectDetection.do_inference(game_screen, return_visualized_image=True)
box_with_max_score = boxes[0][np.argmax(scores)] # [ymin, xmin, ymax, xmax]
apparent_height = (box_with_max_score[2] - box_with_max_score[0])*420.0 # multiplying to de-normalize boxes.
print("\nApparent height = ", apparent_height)
print("\nApparent height (normalized) = ", apparent_height / 420.0)
distance = ((real_height*focal_length)/apparent_height) - focal_length
# velocity = (distance - )/5
print("\nDistance from object = ", distance)
# print(f"Detection Logs [boxes, classes, scores]: {boxes, classes, scores}")
# cv2.imshow('object detection', cv2.resize(image_np, (640,420)))
# image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
# image_np = process_img(image_np)
# image_np = cv2.resize(image_np, (640, 420))
processed_frame, edgy_image, lines = process_img(game_screen)
try:
l1, l2, m1,m2 = draw_lanes(processed_frame,lines)
if brain_ON:
# if distance < 20.0: # 40.0 before
# ReleaseKey(W)
# PressKey(S)
# else:
# PressKey(W)
# ReleaseKey(S)
print("slope1 = ", m1)
print("slope2 = ", m2)
straight()
time_difference = time.time() - time_difference
if time_difference >= 3000.0:
slow_ya_roll()
time.sleep(0.5)
if m1*m2 >= 0.0: # same side
if m1 < 0.0:
right()
# ReleaseKey(A)
# PressKey(W)
# PressKey(D)
# ReleaseKey(W)
else:
left()
# ReleaseKey(D)
# PressKey(W)
# PressKey(A)
# ReleaseKey(W)
except Exception as e:
print("error in brain : ", str(e))
pass
cv2.imshow('object detection', image_with_detections)
# FOR GTA-SA
cv2.imshow('process_img', cv2.resize(edgy_image, (640,420)))
# FOR GTA V
# cv2.imshow('process_img', cv2.resize(edgy_image, (800,600)))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
elif cv2.waitKey(25) & 0xFF == ord('b'):
brain_ON = not brain_ON
print("b pressed.")