# -*- coding: utf-8 -*-
"""
Editor : X-POWER
Date : 2017-03-20
Motto : talk is cheap,show me your code !
This is a temporary script file.
"""
import numpy as np
import pyscreenshot as ImageGrab
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import time
import socket # Network communication
def client(num):
mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysocket.connect(('192.168.1.101',1234))
mysocket.send(str(num))
print("+++++++++++++++++++++++++++++++++++++++")
mysocket.close()
def draw_rectangle(img,location):
cv2.rectangle(img,(640 - location[0],480 - location[1]),(640 - location[2],480 - location[3]),(55,255,155),2) # Belongs to the picture , location , color , thickness
def get_location():
a = [[],[],[],[],[],[],[],[],[],[],[],[]]
rectangle = [(130,180,170,255),(200,185,235,255),(405,205,430,270),(475,210,505,265),(170,115, 200,155),(230,120, 255,165),(385,130, 410,170),(440,130, 470,175),(190,60, 215,100),(245,65, 267,111),(370,75, 393,118),(420,75, 443,118)]
for num in range(12):
for i in range(rectangle[num][0],rectangle[num][2]):
for j in range(rectangle[num][1],rectangle[num][3]):
a[num].append((i,j))
return a,rectangle
def identify_method_one(im,draw_location,rectangle): # image ,
student_location = []
for i in range(12):
sum = 0
for j in draw_location[i]:
tem = im[j[1]][j[0]].sum()/3
tem = abs(im[j[1]][j[0]][0]-tem) + abs(im[j[1]][j[0]][1]-tem) + abs(im[j[1]][j[0]][2]-tem)
sum += tem
sum /= ((rectangle[i][2]-rectangle[i][0]) * (rectangle[i][3] - rectangle[i][1]))
student_location.append(sum)
return student_location
draw_location,rectangle = get_location()
cap = cv2.VideoCapture("http://192.168.1.101:8080/?action=stream")
statue = 0
while True:
ret, frame = cap.read()
im = frame[::-1,::-1,]
student_location = identify_method_one(im,draw_location,rectangle)
count = 0
location = []
for i in student_location:
if i >= 20:
location.append(count)
count += 1
print(student_location)
im = im[::-1,::-1,]
for i in location: #x mark all of the student location
draw_rectangle(frame,rectangle[i])
im = im[::-1,::-1,]
#-------deal-------
num = 0
num1 = 0
num2 = 0
num3 = 0
for i in location:
if i>=0 and i<=3:
num1 = 1
if i>=4 and i<=7:
num2 = 1
if i>=8 and i<=11:
num3 = 1
if num1 == 1:
num += 1
if num2 == 1:
num += 2
if num3 == 1:
num += 4
#-------deal-------
#print(num,statue)
#------Socket-------
if statue != num:
print("---")
statue = num
client(num)
#------Socket-------
cv2.imshow("capture", im)
if cv2.waitKey(1) and 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()