Showing
1 changed file
with
113 additions
and
0 deletions
Modules/edge_cloud.py
0 → 100644
| 1 | +import socket | ||
| 2 | +import cv2 | ||
| 3 | +import numpy as np | ||
| 4 | +from queue import Queue | ||
| 5 | +from _thread import * | ||
| 6 | +enclose_q = Queue() | ||
| 7 | +import time | ||
| 8 | +def filter_img(img): | ||
| 9 | + #이미지의 RGB값을 분석하여 찾는 실내 Tag가 맞는지 판별 | ||
| 10 | + img = cv2.resize(img, (10,10)) | ||
| 11 | + first = [0,0,0] | ||
| 12 | + for x_loc in range(0, 10): | ||
| 13 | + for y_loc in range(0, 10): | ||
| 14 | + bgr_value = img[x_loc,y_loc] | ||
| 15 | + first=first+bgr_value | ||
| 16 | + first[0] = first[0]/100 | ||
| 17 | + first[1] = first[1]/100 | ||
| 18 | + first[2] = first[2]/100 | ||
| 19 | + blue = first[0]<200 and first[0]>120 | ||
| 20 | + green = first[1]>120 and first[1]<210 | ||
| 21 | + red = first[2]>130 and first[2]<230 | ||
| 22 | + | ||
| 23 | + if(blue and green and red): | ||
| 24 | + return True | ||
| 25 | + else: | ||
| 26 | + return False | ||
| 27 | + | ||
| 28 | +def bboxes(inp): | ||
| 29 | + #Frame을 인자로 전달받음 | ||
| 30 | + img = inp | ||
| 31 | + start = time.time() | ||
| 32 | + curTime = time.time() | ||
| 33 | + # img2gray = cv2.imread(fname,0) | ||
| 34 | + # img = cv2.namedWindow(img,cv2.WINDOW_NORMAL) | ||
| 35 | + # img = cv2.resizeWindow(img,600,600) | ||
| 36 | + img_final = inp | ||
| 37 | + img2gray = cv2.cvtColor(inp, cv2.COLOR_BGR2GRAY) #GRAY Image 8bit per pixel | ||
| 38 | + ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY) #threshold : distinguish background, object | ||
| 39 | + image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask) #bitwise | ||
| 40 | + ret, new_img = cv2.threshold(img_final, 180, 255, cv2.THRESH_BINARY) # Nfor black text , cv.THRESH_BINARY_IV | ||
| 41 | + newimg = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY) #Gray Image converting | ||
| 42 | + _,contours, _ = cv2.findContours(newimg, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) # get contours | ||
| 43 | + #cv2.CHAIN_APPROX_NONE: 모든 컨투어 포인트를 반환 | ||
| 44 | + for contour in contours: | ||
| 45 | + [x, y, w, h] = cv2.boundingRect(contour) | ||
| 46 | + | ||
| 47 | + if h / w > 1.0 or w / h > 2.0: | ||
| 48 | + continue | ||
| 49 | + #if h>40 or w>70: | ||
| 50 | + #continue | ||
| 51 | + if y>150: | ||
| 52 | + continue | ||
| 53 | + cropped = img_final[y :y + h , x : x + w] | ||
| 54 | + if(filter_img(cropped)): | ||
| 55 | + cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3) | ||
| 56 | + cv2.putText(img,"cropped", (x-50,y-10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,255), 1) | ||
| 57 | + else: | ||
| 58 | + continue | ||
| 59 | + return cropped | ||
| 60 | + | ||
| 61 | +def threaded(Client_socket, addr, queue): | ||
| 62 | + print("Connected by : ", addr[0], " : ", addr[1]) | ||
| 63 | + while True: | ||
| 64 | + try : | ||
| 65 | + data = Client_socket.recv(1024) | ||
| 66 | + if not data: | ||
| 67 | + print("Disconnected") | ||
| 68 | + break | ||
| 69 | + StringData = queue.get() | ||
| 70 | + Client_socket.send(str(len(StringData)).ljust(16).encode()) | ||
| 71 | + Client_socket.send(StringData) | ||
| 72 | + except ConnectionResetError as e: | ||
| 73 | + print("Disconnected") | ||
| 74 | + Client_socket.close() | ||
| 75 | + | ||
| 76 | + | ||
| 77 | +def webcam(queue): | ||
| 78 | + capture = cv2.VideoCapture(0) | ||
| 79 | + while True: | ||
| 80 | + ret, frame = capture.read() | ||
| 81 | + | ||
| 82 | + if ret == False: | ||
| 83 | + continue | ||
| 84 | + frame = bboxes(frame) | ||
| 85 | + encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90] | ||
| 86 | + result, imgencode = cv2.imencode('.jpg', frame, encode_param) | ||
| 87 | + data = np.array(imgencode) | ||
| 88 | + stringData = data.tostring() | ||
| 89 | + queue.put(stringData) | ||
| 90 | + cv2.imshow('image', frame) | ||
| 91 | + key = cv2.waitKey(1) | ||
| 92 | + if key == 27: | ||
| 93 | + break | ||
| 94 | + | ||
| 95 | +if __name__ == '__main__': | ||
| 96 | + HOST = '127.0.0.1' | ||
| 97 | + PORT = 9999 | ||
| 98 | + | ||
| 99 | + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| 100 | + server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
| 101 | + server_socket.bind((HOST, PORT)) | ||
| 102 | + server_socket.listen() | ||
| 103 | + | ||
| 104 | + print('server start') | ||
| 105 | + | ||
| 106 | + start_new_thread(webcam, (enclose_q,)) | ||
| 107 | + | ||
| 108 | + while True: | ||
| 109 | + print('wait') | ||
| 110 | + | ||
| 111 | + client_socket, addr = server_socket.accept() | ||
| 112 | + start_new_thread(threaded, (client_socket, addr, enclose_q,)) | ||
| 113 | + server_socket.close() |
-
Please register or login to post a comment