Browse Source

Merge remote-tracking branch 'origin/yigit' into efe

yigit
Yiğit Çolakoğlu 6 years ago
parent
commit
69714d6cf8
9 changed files with 947 additions and 53 deletions
  1. +1
    -0
      .gitignore
  2. +88
    -53
      traffic_analyzer/ambulance_detect.py
  3. +20
    -0
      traffic_analyzer/master_app/pom.xml
  4. +24
    -0
      traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java
  5. +258
    -0
      traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form
  6. +425
    -0
      traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java
  7. BIN
      traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/fan.png
  8. +53
    -0
      traffic_analyzer/receive.py
  9. +78
    -0
      traffic_analyzer/sender.py

+ 1
- 0
.gitignore View File

@ -66,3 +66,4 @@ traffic_analyzer/ssd_inception_v2_coco_11_06_2017/
*.iml
traffic_analyzer/rfcn_resnet101_coco_11_06_2017/
/traffic_analyzer/master_app/target/

+ 88
- 53
traffic_analyzer/ambulance_detect.py View File

@ -6,10 +6,18 @@ import sys
import tensorflow as tf
import cv2
from distutils.version import StrictVersion
import socket
from utils import label_map_util
from utils import visualization_utils as vis_util
import psutil
import json
import base64
from PIL import Image
from io import BytesIO
import psutil
switch = 1
import io
@ -70,6 +78,16 @@ TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(
# Size, in inches, of the output images.
sess = 0
switch = 1
data = {"gpu_temp":"10C","gpu_load":"15%","cpu_temp":"47C","cpu_load":"15%","mem_temp":"NaN","mem_load":"17%","fan_speed":"10000RPM"}
def get_temps():
global data
temps = psutil.sensors_temperatures()
data["cpu_temp"] = str(int(temps["dell_smm"][0][1]))+"°C"
data["cpu_load"] = str(psutil.cpu_percent())+"%"
data["mem_load"] = str(dict(psutil.virtual_memory()._asdict())["percent"])+"%"
data["fan_speed"] = str(psutil.sensors_fans()["dell_smm"][0][1])+"RPM"
def run_inference_for_single_image(image, graph):
global switch
global sess
@ -119,63 +137,80 @@ def run_inference_for_single_image(image, graph):
return output_dict
cut=[-225,-1,-225,-1]
cut=[-175,-1,-175,-1]
cut_send = [0,0,0,0]
a = 1
cam = cv2.VideoCapture(1)
conn_switch = False
img_counter = 0
socket_switch = True
cam = cv2.VideoCapture(0)
with detection_graph.as_default():
sess = tf.Session()
switch = 0
switch = 0
get_temps()
while 1:
if(True):
ret,image = cam.read()
image_np = image[cut[0]:cut[1],cut[2]:cut[3]]
#image_np = image_np[int(r[1]):int(r[1]+r[3]),int(r[0]):int(r[0]+r[2])]
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
t1 = time.time()
# Actual detection.
output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8,
min_score_thresh=0.4)
image[cut[0]:cut[1],cut[2]:cut[3]] = image_np
result, frame = cv2.imencode('.jpg', image, encode_param)
data = pickle.dumps(frame, 0)
size = len(data)
if(conn_switch):
pass
else:
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('10.10.26.115', 8488))
connection = client_socket.makefile('wb')
conn_switch = True
except:
pass
try:
client_socket.sendall(struct.pack(">L", size) + data)
except:
conn_switch = False
cv2.imshow("Cam",image)
cv2.imshow("Cut",image_np)
t2 = time.time()
print("time taken for {}".format(t2-t1))
ex_c = [27, ord("q"), ord("Q")]
if cv2.waitKey(1) & 0xFF in ex_c:
break
ret,image = cam.read()
image_np = image[cut[0]:cut[1],cut[2]:cut[3]]
#image_np = image_np[int(r[1]):int(r[1]+r[3]),int(r[0]):int(r[0]+r[2])]
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
t1 = time.time()
# Actual detection.
output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
image[cut[0]:cut[1],cut[2]:cut[3]] = image_np
send_image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
cv2.imshow("Cam",image)
cv2.imshow("Cut",image_np)
if socket_switch:
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 8485))
connection = client_socket.makefile('wb')
socket_switch = False
except:
socket_switch=True
continue
try:
crop_img = send_image.copy(order='C')
crop_img = Image.fromarray(crop_img,"RGB")
buffered = BytesIO()
crop_img.save(buffered, format="JPEG")
img = base64.b64encode(buffered.getvalue()).decode("ascii")
client_socket.sendall(json.dumps({"image_full":img,"image_sizes":{"x":cut_send[2],"y":cut_send[0],"width":cut_send[3],"height":cut_send[1]},"load":data}).encode('gbk')+b"\n")
img_counter += 1
except:
socket_switch=True
if img_counter % 10 ==0:
get_temps()
t2 = time.time()
print("time taken for {}".format(t2-t1))
ex_c = [27, ord("q"), ord("Q")]
if cv2.waitKey(1) & 0xFF in ex_c:
break
except KeyboardInterrupt:
if not socket_switch:
client_socket.sendall(b"Bye\n")
cam.release()
exit(0)
cv2.destroyAllWindows()
cam.release()

+ 20
- 0
traffic_analyzer/master_app/pom.xml View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.yigitcolakoglu</groupId>
<artifactId>master_app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

+ 24
- 0
traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java View File

@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package me.yigitcolakoglu.master_app;
/**
*
* @author yigit
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
cameraForm form = new cameraForm();
form.setVisible(true);
}
}

+ 258
- 0
traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form View File

@ -0,0 +1,258 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<NonVisualComponents>
<Container class="javax.swing.JPanel" name="jPanel1">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="100" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="100" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
</Container>
</NonVisualComponents>
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="camera_full_label" min="-2" pref="900" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="33" max="-2" attributes="0"/>
<Component id="ambulance_button" min="-2" pref="247" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="217" max="-2" attributes="0"/>
<Component id="intersection_button" min="-2" pref="248" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="camera_cut_label" min="-2" pref="300" max="-2" attributes="0"/>
<Group type="103" alignment="0" groupAlignment="1" max="-2" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="jLabel7" min="-2" pref="131" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="fan_rpm" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel1" alignment="0" min="-2" pref="84" max="-2" attributes="0"/>
<Component id="jLabel4" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel5" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel6" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel2" alignment="0" min="-2" pref="97" max="-2" attributes="0"/>
<Component id="gpu_usage" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="cpu_usage" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="ram_usage" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="ram_temp" min="-2" max="-2" attributes="0"/>
<Component id="cpu_temp" min="-2" max="-2" attributes="0"/>
<Component id="gpu_temp" min="-2" max="-2" attributes="0"/>
<Component id="jLabel3" min="-2" pref="84" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
<Component id="bus_button" alignment="0" min="-2" pref="251" max="-2" attributes="0"/>
</Group>
<EmptySpace max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="camera_cut_label" min="-2" pref="300" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="44" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel3" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="18" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel4" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="gpu_usage" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="gpu_temp" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel5" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="cpu_usage" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="cpu_temp" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel6" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="ram_usage" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="ram_temp" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="54" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel7" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="fan_rpm" alignment="3" min="-2" pref="38" max="-2" attributes="0"/>
</Group>
</Group>
<Component id="camera_full_label" min="-2" pref="720" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="intersection_button" alignment="0" min="-2" pref="31" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="ambulance_button" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="bus_button" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace pref="18" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="camera_full_label">
<Properties>
<Property name="text" type="java.lang.String" value=" "/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="camera_cut_label">
<Properties>
<Property name="text" type="java.lang.String" value="camera_cut"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="text" type="java.lang.String" value="Name"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel2">
<Properties>
<Property name="text" type="java.lang.String" value="Usage"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel3">
<Properties>
<Property name="text" type="java.lang.String" value="Temp"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel4">
<Properties>
<Property name="text" type="java.lang.String" value="GPU"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel5">
<Properties>
<Property name="text" type="java.lang.String" value="CPU"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel6">
<Properties>
<Property name="text" type="java.lang.String" value="RAM"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="gpu_usage">
<Properties>
<Property name="text" type="java.lang.String" value="10%"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="cpu_usage">
<Properties>
<Property name="text" type="java.lang.String" value="20%"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="ram_usage">
<Properties>
<Property name="text" type="java.lang.String" value="56%"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="gpu_temp">
<Properties>
<Property name="text" type="java.lang.String" value="10C"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="cpu_temp">
<Properties>
<Property name="text" type="java.lang.String" value="76C"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="ram_temp">
<Properties>
<Property name="text" type="java.lang.String" value="40C"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel7">
<Properties>
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="2" name="/home/yigit/projects/MyCity/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/fan.png"/>
</Property>
<Property name="text" type="java.lang.String" value="jLabel7"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="fan_rpm">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Dialog" size="24" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="2500 RPM"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="ambulance_button">
<Properties>
<Property name="text" type="java.lang.String" value="Ambulance"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="ambulance_buttonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="intersection_button">
<Properties>
<Property name="text" type="java.lang.String" value="Intersection"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="intersection_buttonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="bus_button">
<Properties>
<Property name="text" type="java.lang.String" value="Bus"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="bus_buttonActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Form>

+ 425
- 0
traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java View File

@ -0,0 +1,425 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package me.yigitcolakoglu.master_app;
import java.awt.AlphaComposite;
import java.io.*;
import java.net.*;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Base64;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import org.json.JSONObject;
/**
*
* @author yigit
*/
public class cameraForm extends javax.swing.JFrame {
/**
* Creates new form cameraForm
*/
public cameraForm() {
initComponents();
}
private ServerSocket server;
private Socket client;
private Thread running = null;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
camera_full_label = new javax.swing.JLabel();
camera_cut_label = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
gpu_usage = new javax.swing.JLabel();
cpu_usage = new javax.swing.JLabel();
ram_usage = new javax.swing.JLabel();
gpu_temp = new javax.swing.JLabel();
cpu_temp = new javax.swing.JLabel();
ram_temp = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
fan_rpm = new javax.swing.JLabel();
ambulance_button = new javax.swing.JButton();
intersection_button = new javax.swing.JButton();
bus_button = new javax.swing.JButton();
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
camera_full_label.setText(" ");
camera_cut_label.setText("camera_cut");
jLabel1.setText("Name");
jLabel2.setText("Usage");
jLabel3.setText("Temp");
jLabel4.setText("GPU");
jLabel5.setText("CPU");
jLabel6.setText("RAM");
gpu_usage.setText("10%");
cpu_usage.setText("20%");
ram_usage.setText("56%");
gpu_temp.setText("10C");
cpu_temp.setText("76C");
ram_temp.setText("40C");
jLabel7.setIcon(new javax.swing.ImageIcon("/home/yigit/projects/MyCity/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/fan.png")); // NOI18N
jLabel7.setText("jLabel7");
fan_rpm.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
fan_rpm.setText("2500 RPM");
ambulance_button.setText("Ambulance");
ambulance_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ambulance_buttonActionPerformed(evt);
}
});
intersection_button.setText("Intersection");
intersection_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
intersection_buttonActionPerformed(evt);
}
});
bus_button.setText("Bus");
bus_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bus_buttonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(camera_full_label, javax.swing.GroupLayout.PREFERRED_SIZE, 900, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(ambulance_button, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(217, 217, 217)
.addComponent(intersection_button, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(camera_cut_label, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fan_rpm, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(gpu_usage)
.addComponent(cpu_usage)
.addComponent(ram_usage))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ram_temp)
.addComponent(cpu_temp)
.addComponent(gpu_temp)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addComponent(bus_button, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(camera_cut_label, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(gpu_usage)
.addComponent(gpu_temp))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(cpu_usage)
.addComponent(cpu_temp))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(ram_usage)
.addComponent(ram_temp))
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(fan_rpm, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(camera_full_label, javax.swing.GroupLayout.PREFERRED_SIZE, 720, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(intersection_button, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ambulance_button)
.addComponent(bus_button)))
.addContainerGap(18, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void intersection_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_intersection_buttonActionPerformed
if(running!=null){
try{
server.close();
client.close();
running.stop();
}catch(IOException e){
System.out.println("IO Exception occured");
}catch(Exception e){
System.out.println(e.toString());
}
}
running = new Thread(() -> {
try{
onCreate(8486,"Intersection");
}catch(Exception e){
System.out.println(e.toString());
}
});
running.start();
}//GEN-LAST:event_intersection_buttonActionPerformed
private void ambulance_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ambulance_buttonActionPerformed
if(running!=null){
try{
server.close();
client.close();
running.stop();
}catch(IOException e){
System.out.println("IO Exception occured");
}catch(Exception e){
System.out.println(e.toString());
}
}
running = new Thread(() -> {
try{
onCreate(8485,"Ambulance");
}catch(Exception e){
System.out.println(e.toString());
}
});
running.start();
}//GEN-LAST:event_ambulance_buttonActionPerformed
private void bus_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bus_buttonActionPerformed
if(running!=null){
try{
server.close();
client.close();
running.stop();
}catch(IOException e){
System.out.println("IO Exception occured");
}catch(Exception e){
System.out.println(e.toString());
}
}
running = new Thread(() -> {
try{
onCreate(8487,"Bus");
}catch(Exception e){
System.out.println(e.toString());
}
});
running.start(); }//GEN-LAST:event_bus_buttonActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(cameraForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(cameraForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(cameraForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(cameraForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
System.out.println("Reading: ");
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new cameraForm().setVisible(true);
}});
}
public void onCreate(int port, String name) throws Exception{
this.camera_cut_label.setIcon(new ImageIcon());
this.camera_full_label.setIcon(new ImageIcon());
String fromClient = "";
String toClient;
server = new ServerSocket(port);
System.out.println("wait for connection on port " + port);
boolean run = true;
client = server.accept();
System.out.println("got connection on port " + port);
BufferedImage image = null;
byte[] imageByte;
int null_reps = 0;
while(run) {
try{
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
fromClient = in.readLine();
if(fromClient != null) {
if(fromClient.trim().equals("Bye")) {
run = false;
System.out.println("socket closed");
}else{
System.out.println("received data in size: " + fromClient.length());
JSONObject json = new JSONObject(fromClient);
byte[] decodedBytes = Base64.getDecoder().decode(json.getString("image_full"));
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
image = ImageIO.read(bis);
bis.close();
JSONObject dims = json.getJSONObject("image_sizes");
this.camera_cut_label.setIcon(new ImageIcon(resizeImage(image.getSubimage(dims.getInt("x"), dims.getInt("y"), dims.getInt("width"), dims.getInt("height")),300,300)));
this.camera_full_label.setIcon(new ImageIcon(resizeImage(image,900,720)));
JSONObject data = json.optJSONObject("load");
this.gpu_temp.setText(data.getString("gpu_temp"));
this.gpu_usage.setText(data.getString("gpu_load"));
this.cpu_temp.setText(data.getString("cpu_temp"));
this.cpu_usage.setText(data.getString("cpu_load"));
this.ram_temp.setText(data.getString("mem_temp"));
this.ram_usage.setText(data.getString("mem_load"));
this.fan_rpm.setText(data.getString("fan_speed"));
null_reps=0;
}
}else{
null_reps +=1;
}
}
catch(Exception e){
System.out.println(fromClient);
System.out.println(e.toString());
}
if (null_reps >= 100000){
run = false;
System.out.println("socket closed");
}
}
server.close();
client.close();
JOptionPane.showMessageDialog(this, name +" socket server down!");
}
public static BufferedImage resizeImage(final Image image, int width, int height) {
final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
//below three lines are for RenderingHints for better image quality at cost of higher processing time
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(image, 0, 0, width, height, null);
graphics2D.dispose();
return bufferedImage;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton ambulance_button;
private javax.swing.JButton bus_button;
private javax.swing.JLabel camera_cut_label;
private javax.swing.JLabel camera_full_label;
private javax.swing.JLabel cpu_temp;
private javax.swing.JLabel cpu_usage;
private javax.swing.JLabel fan_rpm;
private javax.swing.JLabel gpu_temp;
private javax.swing.JLabel gpu_usage;
private javax.swing.JButton intersection_button;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel ram_temp;
private javax.swing.JLabel ram_usage;
// End of variables declaration//GEN-END:variables
}

BIN
traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/fan.png View File

Before After
Width: 128  |  Height: 128  |  Size: 5.8 KiB

+ 53
- 0
traffic_analyzer/receive.py View File

@ -0,0 +1,53 @@
import socket
import cv2
import pickle
import struct ## new
HOST=''
PORT=8485
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print('Socket created')
s.bind((HOST,PORT))
print('Socket bind complete')
s.listen(10)
print('Socket now listening')
data = b""
payload_size = struct.calcsize(">L")
print("payload_size: {}".format(payload_size))
switch = True
while True:
while switch:
conn,addr = s.accept()
switch = False
try:
while len(data) < payload_size:
print("Recv: {}".format(len(data)))
data += conn.recv(4096)
print("Done Recv: {}".format(len(data)))
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack(">L", packed_msg_size)[0]
print("msg_size: {}".format(msg_size))
while len(data) < msg_size:
try:
data += conn.recv(4096)
except:
pass
frame_data = data[:msg_size]
data = data[msg_size:]
frame=pickle.loads(frame_data, fix_imports=True, encoding="bytes")
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
cv2.imshow('ImageWindow',frame)
ex_c = [27, ord("q"), ord("Q")]
if cv2.waitKey(1) & 0xFF in ex_c:
break
except:
switch=True

+ 78
- 0
traffic_analyzer/sender.py View File

@ -0,0 +1,78 @@
import cv2
import socket
import json
import base64
from PIL import Image
from io import BytesIO
import psutil
cam = cv2.VideoCapture(0)
img_counter = 0
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
socket_switch = True
cut=[-175,-1,-175,-1]
cut_send = [0,0,0,0]
data = {"gpu_temp":"10C","gpu_load":"15%","cpu_temp":"47C","cpu_load":"15%","mem_temp":"NaN","mem_load":"17%","fan_speed":"10000RPM"}
def get_temps():
global data
temps = psutil.sensors_temperatures()
data["cpu_temp"] = str(int(temps["dell_smm"][0][1]))+"°C"
data["cpu_load"] = str(psutil.cpu_percent())+"%"
data["mem_load"] = str(dict(psutil.virtual_memory()._asdict())["percent"])+"%"
data["fan_speed"] = str(psutil.sensors_fans()["dell_smm"][0][1])+"RPM"
while True:
try:
ret, frame = cam.read()
lens = [len(frame),0,len(frame[0])]
for i in range(0,len(cut),2):
if cut[i]<0:
cut_send[i] = lens[i] + cut[i]
cut_send[i+1] = abs(cut[i])-abs(cut[i+1])
backup = frame
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
crop_img = frame.copy(order='C')
crop_img = Image.fromarray(crop_img,"RGB")
buffered = BytesIO()
crop_img.save(buffered, format="JPEG")
img = base64.b64encode(buffered.getvalue()).decode("ascii")
frame_cut=backup[cut[0]:cut[1],cut[2]:cut[3]]
cv2.imshow("base",backup)
cv2.imshow("cut",frame_cut)
ex_c = [27, ord("q"), ord("Q")]
if cv2.waitKey(1) & 0xFF in ex_c:
break
if socket_switch:
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 8485))
connection = client_socket.makefile('wb')
socket_switch = False
except:
socket_switch=True
continue
try:
client_socket.sendall(json.dumps({"image_full":img,"image_sizes":{"x":cut_send[2],"y":cut_send[0],"width":cut_send[3],"height":cut_send[1]},"load":data}).encode('gbk')+b"\n")
print(img)
except:
socket_switch=True
img_counter += 1
if img_counter % 10 ==0:
get_temps()
except KeyboardInterrupt:
if not socket_switch:
client_socket.sendall(b"Bye\n")
cam.release()
exit(0)

Loading…
Cancel
Save