-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_client.py
72 lines (55 loc) · 2.39 KB
/
main_client.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
#!/usr/bin/env python3
import cv2
import numpy as np
import math
import logging
from cscore import CameraServer
from networktables import NetworkTables
from proc_helper import *
def main():
cs = CameraServer.getInstance()
cs.enableLogging()
camera = cs.startAutomaticCapture()
camera.setResolution(640, 360)
procTable = NetworkTables.getTable("imgproc")
# Get a CvSink. This will capture images from the camera
cvSink = cs.getVideo()
# (optional) Setup a CvSource. This will send images back to the Dashboard
outputStream = cs.putVideo("LQimg", 120, 90)
# Allocating new images is very expensive, always try to preallocate
imgHQ = np.zeros(shape=(640, 360, 3), dtype=np.uint8)
imgLQ = np.zeros(shape=(120, 90, 3), dtype=np.uint8)
while True:
# Tell the CvSink to grab a frame from the camera and put it
# in the source image. If there is an error notify the output.
time, processingImg = cvSink.grabFrame(imgHQ)
if time == 0:
# Send the output the error.
outputStream.notifyError(cvSink.getError())
logging.debug(cvSink.getError())
# skip the rest of the current iteration
continue
# Find suitable contours
contours = detectTargets(processingImg)
goodContours = list(filter(cntTest, contours))
if len(goodContours) >= 1:
rectangledResult = rectangle(processingImg, goodContours)
success, yError,distance = calculateErrors(goodContours)
# Sonuçları robota bildir
procTable.putBoolean('Target algılandı', True)
procTable.putNumber('Horizontal error', yError)
procTable.putNumber('Horizontal error', distance)
else:
rectangledResult = processingImg
procTable.putBoolean('Target algılandı', False)
procTable.putNumber('Horizontal error', 0)
imgLQ = cv2.resize(rectangledResult, (120, 90))
# Give the output stream a new image to display
outputStream.putFrame(imgLQ)
if __name__ == "__main__":
# To see messages from networktables, you must setup logging
logging.basicConfig(filename="/tmp/vision.log", level=logging.DEBUG)
# Join an existing NetworkTables instance (on RoboRIO)
NetworkTables.initialize(server="10.69.85.2")
# TODO call main() after confirming connection
main()