import rw_file as rw daftar = { "l_h_gawang": str(rw.read("setting/LH.txt")), "l_s_gawang": str(rw.read("setting/LS.txt")), "l_v_gawang": str(rw.read("setting/LV.txt")), "u_h_gawang": str(rw.read("setting/UH.txt")), "u_s_gawang": str(rw.read("setting/US.txt")), "u_v_gawang": str(rw.read("setting/UV.txt")), "dilation_gawang": str(rw.read("setting/dilation_gawang.txt")), "dilation_iteration_gawang": str(rw.read("setting/dilation_iteration_gawang.txt")), "erosion_gawang": str(rw.read("setting/erosion_gawang.txt")), "erosion_iteration_gawang": str(rw.read("setting/erosion_iteration_gawang.txt")), "gaussian_gawang": str(rw.read("setting/gaussian_gawang.txt")), "radius_gawang": str(rw.read("setting/radius_gawang.txt")), "l_h_bola": str(rw.read("setting/LH.txt")), "l_s_bola": str(rw.read("setting/LS.txt")),
ap = argparse.ArgumentParser() ap.add_argument("-c", "--camera", type=int, default=0, help="change camera") ap.add_argument("-d", "--display", type=int, default=1, help="Whether or not frame should be displayed") args = ap.parse_args() if args.display > 0: cv.namedWindow("trackbars", cv.WINDOW_NORMAL) cv.resizeWindow("trackbars", 300, 500) cv.createTrackbar("L - H", "trackbars", int(rw.read("setting/LH.txt")), 179, lambda x: rw.write(x, "setting/LH.txt")) cv.createTrackbar("L - S", "trackbars", int(rw.read("setting/LS.txt")), 255, lambda x: rw.write(x, "setting/LS.txt")) cv.createTrackbar("L - V", "trackbars", int(rw.read("setting/LV.txt")), 255, lambda x: rw.write(x, "setting/LV.txt")) cv.createTrackbar("U - H", "trackbars", int(rw.read("setting/UH.txt")), 179, lambda x: rw.write(x, "setting/UH.txt")) cv.createTrackbar("U - S", "trackbars", int(rw.read("setting/US.txt")), 255, lambda x: rw.write(x, "setting/US.txt")) cv.createTrackbar("U - V", "trackbars", int(rw.read("setting/UV.txt")), 255, lambda x: rw.write(x, "setting/UV.txt")) cv.createTrackbar("dilation", "trackbars", int(rw.read("setting/dilation_bola.txt")), 50, lambda x: rw.write(x, "setting/dilation_bola.txt"))
mask = cv.erode(mask, kernel) _, contours, _ = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) if len(contours) > 0: radius_gawang, center_gawang, cx_gawang, cy_gawang, _, _ = centroid(contours) return(radius_gawang, center_gawang, cx_gawang, cy_gawang) cap = cv.VideoCapture(0) cap.set(cv.CAP_PROP_FRAME_WIDTH, 120) cap.set(cv.CAP_PROP_FRAME_HEIGHT, 240) cv.namedWindow("trackbars", cv.WINDOW_NORMAL) cv.resizeWindow("trackbars", 300, 500) cv.createTrackbar("L - H", "trackbars", int(rw.read("setting/LH.txt")), 179, lambda x: rw.write(x, "setting/LH.txt")) cv.createTrackbar("L - S", "trackbars", int(rw.read("setting/LS.txt")), 255, lambda x : rw.write(x, "setting/LS.txt")) cv.createTrackbar("L - V", "trackbars", int(rw.read("setting/LV.txt")), 255, lambda x : rw.write(x, "setting/LV.txt")) cv.createTrackbar("U - H", "trackbars", int(rw.read("setting/UH.txt")), 179, lambda x: rw.write(x, "setting/UH.txt")) cv.createTrackbar("U - S", "trackbars", int(rw.read("setting/US.txt")), 255, lambda x: rw.write(x, "setting/US.txt")) cv.createTrackbar("U - V", "trackbars", int(rw.read("setting/UV.txt")), 255, lambda x : rw.write(x, "setting/UV.txt")) cv.createTrackbar("dilation", "trackbars", int(rw.read("setting/dilation_bola.txt")), 50, lambda x : rw.write(x, "setting/dilation_bola.txt")) cv.createTrackbar("Dilation iterations", "trackbars", int(rw.read("setting/dilation_iteration_bola.txt")), 200, lambda x : rw.write(x, "setting/dilation_iteration_bola.txt")) cv.createTrackbar("erosion", "trackbars", int(rw.read("setting/erosion_bola.txt")), 50, lambda x : rw.write(x, "setting/erosion_bola.txt")) cv.createTrackbar("Erosion iterations", "trackbars", int(rw.read("setting/erosion_iteration_bola.txt")), 200, lambda x : rw.write(x, "setting/erosion_iteration_bola.txt")) cv.createTrackbar("gaussian", "trackbars", int(rw.read("setting/gaussian_bola.txt")), 20, lambda x : rw.write(x, "setting/gaussian_bola.txt")) cv.createTrackbar("radius", "trackbars", int(rw.read("setting/radius_bola.txt")), 50, lambda x : rw.write(x, "setting/radius_bola.txt"))
#TODO bikin argument parser #TODO bikin function buat masukin semua parameter ke array ap = argparse.ArgumentParser() ap.add_argument("-c", "--camera", type=int, default=0, help="change camera") ap.add_argument("-d", "--display", type=int, default=-1, help="Whether or not frame should be displayed") args = ap.parse_args() l_h_gawang = int(rw.read("setting/LH_gawang.txt")) l_s_gawang = int(rw.read("setting/LS_gawang.txt")) l_v_gawang = int(rw.read("setting/LV_gawang.txt")) u_h_gawang = int(rw.read("setting/UH_gawang.txt")) u_s_gawang = int(rw.read("setting/US_gawang.txt")) u_v_gawang = int(rw.read("setting/UV_gawang.txt")) dilation_gawang = int(rw.read("setting/dilation_gawang.txt")) dilation_iteration_gawang = int( rw.read("setting/dilation_iteration_gawang.txt")) erosion_gawang = int(rw.read("setting/erosion_gawang.txt")) erosion_iteration_gawang = int(rw.read("setting/erosion_iteration_gawang.txt")) gaussian_gawang = int(rw.read("setting/gaussian_gawang.txt")) radius_gawang = int(rw.read("setting/radius_gawang.txt")) l_h_bola = int(rw.read("setting/LH.txt"))
w = 160 h = 160 cap = cv2.VideoCapture(0) cap.set(3, w) cap.set(4, h) cv2.namedWindow('HueComp') cv2.namedWindow('SatComp') cv2.namedWindow('ValComp') cv2.namedWindow('closing') cv2.namedWindow('mask') cv2.namedWindow('tracking') cv2.createTrackbar('hmin', 'HueComp', int(rw.read("setting/LH.txt")), 179, lambda x: rw.write(x, "setting/LH.txt")) cv2.createTrackbar('hmax', 'HueComp', int(rw.read("setting/UH.txt")), 179, lambda x: rw.write(x, "setting/UH.txt")) cv2.createTrackbar('smin', 'SatComp', int(rw.read("setting/LS.txt")), 255, lambda x: rw.write(x, "setting/LS.txt")) cv2.createTrackbar('smax', 'SatComp', int(rw.read("setting/US.txt")), 255, lambda x: rw.write(x, "setting/US.txt")) cv2.createTrackbar('vmin', 'ValComp', int(rw.read("setting/LV.txt")), 255, lambda x: rw.write(x, "setting/LV.txt")) cv2.createTrackbar('vmax', 'ValComp', int(rw.read("setting/UV.txt")), 255, lambda x: rw.write(x, "setting/UV.txt")) while (1):
import rw_file as rw import cv2 as cv import imutils from imutils.video import WebcamVideoStream import numpy as np import argparse ### LOAD CONFIGURATION ### l_h_gawang = int(rw.read("setting/LH_gawang.txt")) l_s_gawang = int(rw.read("setting/LS_gawang.txt")) l_v_gawang = int(rw.read("setting/LV_gawang.txt")) u_h_gawang = int(rw.read("setting/UH_gawang.txt")) u_s_gawang = int(rw.read("setting/US_gawang.txt")) u_v_gawang = int(rw.read("setting/UV_gawang.txt")) lower_white = np.array([l_h_gawang, l_s_gawang, l_v_gawang]) upper_white = np.array([u_h_gawang, u_s_gawang, u_v_gawang]) dilation_gawang = rw.odd(int(rw.read("setting/dilation_gawang.txt"))) dilation_iteration_gawang = int( rw.read("setting/dilation_iteration_gawang.txt")) erosion_gawang = rw.odd(int(rw.read("setting/erosion_gawang.txt"))) erosion_iteration_gawang = int(rw.read("setting/erosion_iteration_gawang.txt")) gaussian_gawang = rw.odd(int(rw.read("setting/gaussian_gawang.txt"))) radius_gawang = int(rw.read("setting/radius_gawang.txt")) erosion_kernel = cv.getStructuringElement(cv.MORPH_RECT, (erosion_gawang, erosion_gawang)) dilation_kernel = cv.getStructuringElement(cv.MORPH_RECT, (dilation_gawang, dilation_gawang))
def update(self, frame, framecenter): frame = imutils.resize(frame, width=300) gaussian_kernel = int(rw.read("setting/gaussian_bola.txt")) gaussian_kernel = rw.odd(gaussian_kernel) frame = cv.GaussianBlur(frame, (gaussian_kernel, gaussian_kernel), 0) hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) l_h = int(rw.read("setting/LH.txt")) l_s = int(rw.read("setting/LS.txt")) l_v = int(rw.read("setting/LV.txt")) u_h = int(rw.read("setting/UH.txt")) u_s = int(rw.read("setting/US.txt")) u_v = int(rw.read("setting/UV.txt")) lower_white = np.array([l_h, l_s, l_v]) upper_white = np.array([u_h, u_s, u_v]) mask = cv.inRange(hsv, lower_white, upper_white) erosion = int(rw.read("setting/erosion_bola.txt")) erosion = rw.odd(erosion) dilation = int(rw.read("setting/dilation_bola.txt")) dilation = rw.odd(dilation) erosion_iterations = int(rw.read("setting/erosion_iteration_bola.txt")) dilation_iterations = int( rw.read("setting/dilation_iteration_bola.txt")) erosion_kernel = cv.getStructuringElement(cv.MORPH_RECT, (erosion, erosion)) dilation_kernel = cv.getStructuringElement(cv.MORPH_RECT, (dilation, dilation)) mask = cv.erode(mask, erosion_kernel, iterations=erosion_iterations) mask = cv.dilate(mask, dilation_kernel, iterations=dilation_iterations) # result = cv.bitwise_and(frame, frame, mask = mask) contours, _ = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) x = 0 y = 0 radius = 0 if len(contours) > 0: c = max(contours, key=cv.contourArea) ((x, y), radius) = cv.minEnclosingCircle(c) M = cv.moments(c) if int(M["m00"]) > 0: rads = int(rw.read("setting/radius_bola.txt")) if radius > rads: return ((int(x), int(y))) return (framecenter)
from imutils.video import WebcamVideoStream ap = argparse.ArgumentParser() ap.add_argument("-c", "--camera", type=int, default=0, help="change camera") ap.add_argument("-d", "--display", type=int, default=1, help="Whether or not frame should be displayed") args = ap.parse_args() cv.namedWindow("trackbars", cv.WINDOW_NORMAL) cv.resizeWindow("trackbars", 300, 500) cv.createTrackbar("L - H", "trackbars", int(rw.read("setting/LH_gawang.txt")), 179, lambda x: rw.write(x, "setting/LH_gawang.txt")) cv.createTrackbar("L - S", "trackbars", int(rw.read("setting/LS_gawang.txt")), 255, lambda x: rw.write(x, "setting/LS_gawang.txt")) cv.createTrackbar("L - V", "trackbars", int(rw.read("setting/LV_gawang.txt")), 255, lambda x: rw.write(x, "setting/LV_gawang.txt")) cv.createTrackbar("U - H", "trackbars", int(rw.read("setting/UH_gawang.txt")), 179, lambda x: rw.write(x, "setting/UH_gawang.txt")) cv.createTrackbar("U - S", "trackbars", int(rw.read("setting/US_gawang.txt")), 255, lambda x: rw.write(x, "setting/US_gawang.txt")) cv.createTrackbar("U - V", "trackbars", int(rw.read("setting/UV_gawang.txt")), 255, lambda x: rw.write(x, "setting/UV_gawang.txt")) cv.createTrackbar("dilation", "trackbars", int(rw.read("setting/dilation_gawang.txt")), 20, lambda x: rw.write(x, "setting/dilation_gawang.txt"))