Browse Source

init white_list

jezvgg 11 months ago
parent
commit
0f8bf30a1d
2 changed files with 106 additions and 0 deletions
  1. 64 0
      white_list/main.py
  2. 42 0
      white_list/nemain.py

+ 64 - 0
white_list/main.py

@@ -0,0 +1,64 @@
+import zmq
+import cv2
+import numpy as np
+
+context = zmq.Context()
+socket = context.socket(zmq.SUB)
+socket.setsockopt(zmq.SUBSCRIBE, b"")
+port = 5055
+socket.connect(f"tcp://192.168.0.113:{port}")
+n = 0
+
+cv2.namedWindow('Image', cv2.WINDOW_GUI_NORMAL)
+cv2.namedWindow('Debug', cv2.WINDOW_GUI_NORMAL)
+
+lower = 100
+upper = 200
+
+
+def lower_update(value):
+    global lower
+    lower = value
+
+
+def upper_update(value):
+    global upper
+    upper = value
+
+
+cv2.createTrackbar("Lower", "Debug", lower, 255, lower_update)
+cv2.createTrackbar("Upper", "Debug", upper, 255, upper_update)
+
+while True:
+    bts = socket.recv()
+    n += 1
+    arr = np.frombuffer(bts, np.uint8)
+    image = cv2.imdecode(arr, cv2.IMREAD_COLOR)
+    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+    gray = cv2.GaussianBlur(gray, (7,7), 0)
+    mask = cv2.Canny(gray, lower, upper)
+    cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
+
+    rect = cv2.minAreaRect(cnts[0])
+    box = cv2.boxPoints(rect)
+    box = np.intp(box)
+
+    approx = cv2.approxPolyDP(box, 0.3, True)
+
+    A4_shape = np.float32([[0,870], [0, 0], [620,0], [620,877]])
+    A4 = np.zeros((870, 620))
+    mask = cv2.getPerspectiveTransform(approx[:, 0, :].astype('float32'), A4_shape)
+    only_list = cv2.warpPerspective(image, mask, (640, 480))
+    cv2.putText(only_list, "Hello world", (10, 60), cv2.FONT_HERSHEY_SIMPLEX,
+                0.7, (127, 255, 255))
+    mask = cv2.getPerspectiveTransform(A4_shape, approx[:, 0, :].astype('float32'))
+    listText = cv2.warpPerspective(only_list, mask, (640, 480))
+    listText[np.all(listText < 150, axis=2)] = image[np.all(listText < 150, axis=2)]
+
+    for pos in approx:
+        cv2.circle(image, pos[0], 2, (0, 255, 0), 10)
+
+    cv2.imshow('Image', listText)
+    key = cv2.waitKey(10)
+    if key == ord('q'):
+        break

+ 42 - 0
white_list/nemain.py

@@ -0,0 +1,42 @@
+
+import cv2
+import numpy as np
+
+camera = cv2.VideoCapture(0)
+# camera.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
+# camera.set(cv2.CAP_PROP_EXPOSURE, -5)
+
+cv2.namedWindow('Image', cv2.WINDOW_GUI_NORMAL)
+cv2.namedWindow('Debug', cv2.WINDOW_GUI_NORMAL)
+
+lower = 100
+upper = 200
+
+
+def lower_update(value):
+    global lower
+    lower = value
+
+
+def upper_update(value):
+    global upper
+    upper = value
+
+
+cv2.createTrackbar("Lower", "Debug", lower, 255, lower_update)
+cv2.createTrackbar("Upper", "Debug", upper, 255, upper_update)
+
+while camera.isOpened():
+    ret, image = camera.read()
+    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+    gray = cv2.GaussianBlur(gray, (7,7), 0)
+    mask = cv2.Canny(gray, lower, upper)
+    cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
+    cv2.drawContours(image, cnts, -1, (0,255,0), 10)
+    print(mask.shape)
+    print(image.shape)
+    cv2.imshow('Image', image)
+    cv2.imshow('Debug', mask)
+    key = cv2.waitKey(10)
+    if key == ord('q'):
+        break