Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Bug/load file to main #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion findTargetS.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def findValids(img_orig, filename, debug):
# Make copy of frame/image to work with
img = np.copy(img_orig)

# Load calibration parameters
# LOADING CALIBRATION FROM FILE -- TO FIX -- THIS IS A TEXT FILE
values = np.load(filename)
brightness = float(values['brightness'])
lower_bound = values['lower']
Expand Down
111 changes: 59 additions & 52 deletions mainSimple.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,94 @@
import NetworkTableModule as NT
import cv2
import time

directory = 'C:/Users/Ithier/Documents/FIRST/2017/Off Season/'
filename = directory + 'imageValues.npz' # folder npz file is in. NPZ file contains hsv values and brightness value
url = 0
debug = 1
validCount = 0 # how many valid targets we've found
n = 0
freqFramesNT = 10 # how often we're sending to network tables

#############################################################################
from networktables import NetworkTable
import logging
import numpy as np
import sys

url = 0 # CAMERA ADDRESS, 0 is local on Windows
debug = 1 # debug level
validCount = 0 # how many valid targets we've found
n = 0 # iterator
freqFramesNT = 10 # send to NetworkTables every X frames

#if NetworkTable._staticProvider is None:
try:
logging.basicConfig(level=logging.DEBUG)
NetworkTable.setIPAddress('10.5.1.2')
NetworkTable.setClientMode()
NetworkTable.initialize()
except:
if debug == 1:
print("Network tables has already been initialized")
logging.basicConfig(level=logging.DEBUG)

def load_npz_file():
"""
loads npz file (camera calibration)
returns float,int,int
"""
filename = "imageValues.npz" # folder npz file is in. NPZ file contains hsv values and brightness value
values = np.load(filename)
brightness = float(values['brightness'])
lower_bound = values['lower']
upper_bound = values['upper']
return brightness,lower_bound,upper_bound

sd = NetworkTable.getTable("Camera")
#############################################################################
cap = cv2.VideoCapture(url) # capture camera, 0 is laptop cam, numbers after that are cameras attached
time.sleep(2)
# Check to make sure cap was initialized in capture
if debug:
if cap.isOpened():
print('Cap succesfully opened')
print(cap.grab())
else:
print('Cap initialization failed')

# Create resizable window for camera
cv2.namedWindow('Camera Frame', cv2.WINDOW_NORMAL)
def init_network_tables():
try:
NetworkTable.setIPAddress('10.5.1.2')
NetworkTable.setClientMode()
NetworkTable.initialize()
sd = NetworkTable.getTable("Camera")
except:
if debug == 1:
print("Unable to initialize NetworkTables, exiting.")
with open("vision_error.log", "w") as text_file:
text_file.write("unable to initialize networktables. exited.")
sys.exit()
return sd

while(cap.isOpened()):
# Capture frame-by-frame
# ret returns true or false (T if img read correctly); frame is array of img
def init_capture():
cap = cv2.VideoCapture(url) # capture camera, 0 is laptop cam, numbers after that are cameras attached
time.sleep(2)
# Check to make sure cap was initialized in capture
if debug:
if cap.isOpened():
print('Capture Initialized.')
else:
print('Capture Failed.')

while(cap.isOpened()):
ret, frame = cap.read()

if ret == True: # if frame succesfully read
if frame is None: # if no frame print a blank image
if debug:
print('Frame is None')
Processed_frame = cv2.imread('1.png', 1)
mask = Processed_frame

else:
try:
# Process image
Angle, Distance, validUpdate, Processed_frame, mask, cnt = FT.findValids(frame, filename, debug)
if validUpdate:

if validUpdate:
validCount += 1

if n > freqFramesNT:
# Send to NetworkTable
NT.sendValues(sd, Angle, Distance, validCount)
n = 0
else:
n += 1

except:
Processed_frame = cv2.imread('1.jpg', 1)
mask = Processed_frame
if debug:
print('There was an error with findValids')
if debug:

#if debug:
# Display the resulting frame
cv2.imshow('Camera Frame', Processed_frame)
cv2.imshow('Mask', mask)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
# cv2.imshow('Camera Frame', Processed_frame)
# cv2.imshow('Mask', mask)

# if cv2.waitKey(5) & 0xFF == ord('q'):
# break

# When capture done, release it
cap.release() # !! important to do
if debug:
cv2.destroyAllWindows()
#if debug:
# cv2.destroyAllWindows()