From 2898bd37ca6757ca703523814638ef2315dec003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Sat, 18 May 2019 23:36:06 +0300 Subject: [PATCH 1/3] added app, file sending complete --- .gitignore | 1 + traffic_analyzer/ambulance_detect.py | 3 +- traffic_analyzer/master_app/pom.xml | 20 ++ .../me/yigitcolakoglu/master_app/Main.java | 30 ++ .../yigitcolakoglu/master_app/cameraForm.form | 215 ++++++++++++ .../yigitcolakoglu/master_app/cameraForm.java | 306 ++++++++++++++++++ .../java/me/yigitcolakoglu/master_app/fan.png | Bin 0 -> 5943 bytes traffic_analyzer/receive.py | 53 +++ traffic_analyzer/sender.py | 79 +++++ 9 files changed, 706 insertions(+), 1 deletion(-) create mode 100644 traffic_analyzer/master_app/pom.xml create mode 100644 traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java create mode 100644 traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form create mode 100644 traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java create mode 100644 traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/fan.png create mode 100644 traffic_analyzer/receive.py create mode 100644 traffic_analyzer/sender.py diff --git a/.gitignore b/.gitignore index 6caee12..5171450 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ \ No newline at end of file diff --git a/traffic_analyzer/ambulance_detect.py b/traffic_analyzer/ambulance_detect.py index 3ae8166..bead0ed 100644 --- a/traffic_analyzer/ambulance_detect.py +++ b/traffic_analyzer/ambulance_detect.py @@ -143,7 +143,8 @@ while 1: use_normalized_coordinates=True, line_thickness=8) image[cut[0]:cut[1],cut[2]:cut[3]] = image_np - cv2.imshow("Cam",np.concatenate((image,image_np),axis=0)) + cv2.imshow("Cam",image) + cv2.imshow("Cut",image_np) t2 = time.time() print("time taken for {}".format(t2-t1)) diff --git a/traffic_analyzer/master_app/pom.xml b/traffic_analyzer/master_app/pom.xml new file mode 100644 index 0000000..76f39b2 --- /dev/null +++ b/traffic_analyzer/master_app/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + me.yigitcolakoglu + master_app + 1.0-SNAPSHOT + jar + + + org.json + json + 20180813 + + + + UTF-8 + 11 + 11 + + \ No newline at end of file diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java new file mode 100644 index 0000000..373f8bc --- /dev/null +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java @@ -0,0 +1,30 @@ +/* + * 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); + try{ + while(true){ + form.onCreate(); + } + }catch(Exception e){ + System.out.println(e.toString()); + } + } + +} diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form new file mode 100644 index 0000000..a66a5c7 --- /dev/null +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form @@ -0,0 +1,215 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java new file mode 100644 index 0000000..47c350f --- /dev/null +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java @@ -0,0 +1,306 @@ +/* + * 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 org.json.JSONObject; + +/** + * + * @author yigit + */ +public class cameraForm extends javax.swing.JFrame { + + /** + * Creates new form cameraForm + */ + public cameraForm() { + initComponents(); + } + + /** + * 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") + // //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(); + + 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_full"); + + 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"); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + 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) + .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))))) + .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)) + .addContainerGap(67, Short.MAX_VALUE)) + ); + + pack(); + }// //GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* 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); + } + // + 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() throws Exception{ + String fromClient; + String toClient; + + ServerSocket server = new ServerSocket(8485); + System.out.println("wait for connection on port 8080"); + + boolean run = true; + Socket client = server.accept(); + System.out.println("got connection on port 8080"); + 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) { + 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")); + if(fromClient.equals("Bye")) { + client.close(); + run = false; + System.out.println("socket closed"); + System.exit(0); + } + null_reps=0; + }else{ + null_reps +=1; + } + } + catch(Exception e){ + System.out.println(e.toString()); + } + if (null_reps >= 1000){ + break; + } + } + + } + + 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.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.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 +} diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/fan.png b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/fan.png new file mode 100644 index 0000000000000000000000000000000000000000..994a1153fd01ace8ff9f475ed86e96691a730803 GIT binary patch literal 5943 zcmZ{IS2!Dv_kQfyv-X}r5qqmGi5Z(xBUXGgwxDXn2(ed*ReQHqwdf+m-rAy6soiR8 zRZ-fX@8$pEe{s(Hobf*I^PH=5PMVd42|W!r4FCY3he8akZ!rFU1|+}nNYy6Y8zA@9 zGuHzEIJ9*C6axSpL1le#t(yX= zr>Th{0MMK%qHq&Y2SJ=dZ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{bA62=(25z5tw`{(u8&je5DT(X#xx0?L2oJIzWL*ZMWPJh5 zzj0Ho{B&cWaT#@ok9QMHwan2k`-QI{Pj?vEHah@6J60*Zkp4Nh;kFh%W~{TTm=I>n zWGZks($TqmyHp=4R6bXm3%Lz<>~J5#VE^j93g(znVvxE{1PPQQc~Q5UMV-~5Qh!i*flygb6ZH4q`*Hvmuo%d5+Pm1X4BY!&1+z-k&`6=|@7 z1{i$4;`Q$TA>0r2Li$Ag{|RX+t9~~GuKy|61p0)bJwm(zXf#?DiTW=T;SuC58yMnS T`a_HRrU?KwvM_wA?~eIDi&n{x literal 0 HcmV?d00001 diff --git a/traffic_analyzer/receive.py b/traffic_analyzer/receive.py new file mode 100644 index 0000000..490cb69 --- /dev/null +++ b/traffic_analyzer/receive.py @@ -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 \ No newline at end of file diff --git a/traffic_analyzer/sender.py b/traffic_analyzer/sender.py new file mode 100644 index 0000000..3899685 --- /dev/null +++ b/traffic_analyzer/sender.py @@ -0,0 +1,79 @@ +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] + +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]) + 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=frame[cut[0]:cut[1],cut[2]:cut[3]] + cv2.imshow("base",frame) + cv2.imshow("cut",frame_cut) + cv2.imshow("test", frame[cut_send[0]:cut_send[0]+cut_send[1],cut_send[2]:cut_send[2]+cut_send[3]] + ) + ex_c = [27, ord("q"), ord("Q")] + if cv2.waitKey(1) & 0xFF in ex_c: + break + if 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') + switch = False + except: + 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: + switch=True + img_counter += 1 + if img_counter % 30 ==0: + get_temps() + except KeyboardInterrupt: + if not switch: + client_socket.sendall(b"Bye") + cam.release() + exit(0) + + + + From 9d8b8e0b316a2ddc17eb6dd07e4c8dccac0f7997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Sun, 19 May 2019 10:54:54 +0300 Subject: [PATCH 2/3] applied socket changes to ambulance detector !!!Not tested probably will give an error refer to sender.py for fix --- traffic_analyzer/ambulance_detect.py | 122 ++++++++++++------ .../yigitcolakoglu/master_app/cameraForm.form | 45 ++++++- .../yigitcolakoglu/master_app/cameraForm.java | 104 ++++++++++----- traffic_analyzer/sender.py | 23 ++-- 4 files changed, 209 insertions(+), 85 deletions(-) diff --git a/traffic_analyzer/ambulance_detect.py b/traffic_analyzer/ambulance_detect.py index bead0ed..854ff20 100644 --- a/traffic_analyzer/ambulance_detect.py +++ b/traffic_analyzer/ambulance_detect.py @@ -2,20 +2,22 @@ import numpy as np import os -import six.moves.urllib as urllib import sys -import tarfile import tensorflow as tf -import zipfile import cv2 from distutils.version import StrictVersion -from collections import defaultdict -from io import StringIO - +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 # This is needed since the notebook is stored in the object_detection folder. sys.path.append("..") @@ -65,6 +67,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 @@ -113,43 +125,79 @@ def run_inference_for_single_image(image, graph): output_dict['detection_masks'] = output_dict['detection_masks'][0] return output_dict cut=[-175,-1,-175,-1] +cut_send = [0,0,0,0] a = 1 +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): + try: + 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) - 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 - 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 cv2.destroyAllWindows() cam.release() diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form index a66a5c7..035d0bc 100644 --- a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form @@ -40,9 +40,19 @@ - - - + + + + + + + + + + + + + @@ -75,6 +85,7 @@ + @@ -119,7 +130,13 @@ - + + + + + + + @@ -127,7 +144,7 @@ - + @@ -211,5 +228,23 @@ + + + + + + + + + + + + + + + + + + diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java index 47c350f..282d1c6 100644 --- a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java @@ -14,6 +14,7 @@ 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; /** @@ -55,6 +56,9 @@ public class cameraForm extends javax.swing.JFrame { ram_temp = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); fan_rpm = new javax.swing.JLabel(); + jButton1 = new javax.swing.JButton(); + jButton2 = new javax.swing.JButton(); + jButton3 = new javax.swing.JButton(); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); @@ -69,7 +73,7 @@ public class cameraForm extends javax.swing.JFrame { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - camera_full_label.setText("camera_full"); + camera_full_label.setText(" "); camera_cut_label.setText("camera_cut"); @@ -103,13 +107,31 @@ public class cameraForm extends javax.swing.JFrame { fan_rpm.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N fan_rpm.setText("2500 RPM"); + jButton1.setText("Ambulance"); + + jButton2.setText("Intersection"); + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); + + jButton3.setText("Bus"); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( 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.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(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(217, 217, 217) + .addComponent(jButton2, 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) @@ -135,7 +157,8 @@ public class cameraForm extends javax.swing.JFrame { .addComponent(ram_temp) .addComponent(cpu_temp) .addComponent(gpu_temp) - .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))))) + .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( @@ -170,12 +193,21 @@ public class cameraForm extends javax.swing.JFrame { .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)) - .addContainerGap(67, Short.MAX_VALUE)) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton1) + .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jButton3)) + .addContainerGap(18, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_jButton2ActionPerformed + /** * @param args the command line arguments */ @@ -213,7 +245,9 @@ public class cameraForm extends javax.swing.JFrame { } public void onCreate() throws Exception{ - String fromClient; + this.camera_cut_label.setIcon(new ImageIcon()); + this.camera_full_label.setIcon(new ImageIcon()); + String fromClient = ""; String toClient; ServerSocket server = new ServerSocket(8485); @@ -225,50 +259,55 @@ public class cameraForm extends javax.swing.JFrame { 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) { - 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")); - if(fromClient.equals("Bye")) { - client.close(); + if(fromClient.trim().equals("Bye")) { run = false; System.out.println("socket closed"); - System.exit(0); + }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; } - null_reps=0; }else{ null_reps +=1; } } catch(Exception e){ + System.out.println(fromClient); System.out.println(e.toString()); } - if (null_reps >= 1000){ - break; + if (null_reps >= 100000){ + run = false; + System.out.println("socket closed"); } } - + server.close(); + client.close(); + JOptionPane.showMessageDialog(this, "Ambulance socket server down!"); + } public static BufferedImage resizeImage(final Image image, int width, int height) { @@ -292,6 +331,9 @@ public class cameraForm extends javax.swing.JFrame { private javax.swing.JLabel fan_rpm; private javax.swing.JLabel gpu_temp; private javax.swing.JLabel gpu_usage; + private javax.swing.JButton jButton1; + private javax.swing.JButton jButton2; + private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; diff --git a/traffic_analyzer/sender.py b/traffic_analyzer/sender.py index 3899685..deea7e7 100644 --- a/traffic_analyzer/sender.py +++ b/traffic_analyzer/sender.py @@ -13,7 +13,7 @@ img_counter = 0 encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90] -switch = True +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"} @@ -35,6 +35,7 @@ while True: 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') @@ -42,35 +43,33 @@ while True: buffered = BytesIO() crop_img.save(buffered, format="JPEG") img = base64.b64encode(buffered.getvalue()).decode("ascii") - frame_cut=frame[cut[0]:cut[1],cut[2]:cut[3]] - cv2.imshow("base",frame) + frame_cut=backup[cut[0]:cut[1],cut[2]:cut[3]] + cv2.imshow("base",backup) cv2.imshow("cut",frame_cut) - cv2.imshow("test", frame[cut_send[0]:cut_send[0]+cut_send[1],cut_send[2]:cut_send[2]+cut_send[3]] - ) ex_c = [27, ord("q"), ord("Q")] if cv2.waitKey(1) & 0xFF in ex_c: break - if switch: + 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') - switch = False + socket_switch = False except: - switch=True + 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: - switch=True + socket_switch=True img_counter += 1 - if img_counter % 30 ==0: + if img_counter % 10 ==0: get_temps() except KeyboardInterrupt: - if not switch: - client_socket.sendall(b"Bye") + if not socket_switch: + client_socket.sendall(b"Bye\n") cam.release() exit(0) From 215ea2a92333aef0d1981448291af800db9c01a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Sun, 19 May 2019 11:39:13 +0300 Subject: [PATCH 3/3] finished the master gui --- .../me/yigitcolakoglu/master_app/Main.java | 8 +- .../yigitcolakoglu/master_app/cameraForm.form | 30 ++-- .../yigitcolakoglu/master_app/cameraForm.java | 133 ++++++++++++++---- 3 files changed, 125 insertions(+), 46 deletions(-) diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java index 373f8bc..9c9f1c3 100644 --- a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/Main.java @@ -18,13 +18,7 @@ public class Main { // TODO code application logic here cameraForm form = new cameraForm(); form.setVisible(true); - try{ - while(true){ - form.onCreate(); - } - }catch(Exception e){ - System.out.println(e.toString()); - } + } } diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form index 035d0bc..990b647 100644 --- a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.form @@ -48,9 +48,9 @@ - + - + @@ -85,7 +85,7 @@ - + @@ -131,10 +131,12 @@ - - - - + + + + + + @@ -228,23 +230,29 @@ - + + + + - + - + - + + + + diff --git a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java index 282d1c6..dd4ea64 100644 --- a/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java +++ b/traffic_analyzer/master_app/src/main/java/me/yigitcolakoglu/master_app/cameraForm.java @@ -29,7 +29,9 @@ public class cameraForm extends javax.swing.JFrame { 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 @@ -56,9 +58,9 @@ public class cameraForm extends javax.swing.JFrame { ram_temp = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); fan_rpm = new javax.swing.JLabel(); - jButton1 = new javax.swing.JButton(); - jButton2 = new javax.swing.JButton(); - jButton3 = new javax.swing.JButton(); + 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); @@ -107,16 +109,26 @@ public class cameraForm extends javax.swing.JFrame { fan_rpm.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N fan_rpm.setText("2500 RPM"); - jButton1.setText("Ambulance"); + ambulance_button.setText("Ambulance"); + ambulance_button.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + ambulance_buttonActionPerformed(evt); + } + }); - jButton2.setText("Intersection"); - jButton2.addActionListener(new java.awt.event.ActionListener() { + intersection_button.setText("Intersection"); + intersection_button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton2ActionPerformed(evt); + intersection_buttonActionPerformed(evt); } }); - jButton3.setText("Bus"); + 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); @@ -129,9 +141,9 @@ public class cameraForm extends javax.swing.JFrame { .addComponent(camera_full_label, javax.swing.GroupLayout.PREFERRED_SIZE, 900, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(33, 33, 33) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(ambulance_button, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(217, 217, 217) - .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))) + .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) @@ -158,7 +170,7 @@ public class cameraForm extends javax.swing.JFrame { .addComponent(cpu_temp) .addComponent(gpu_temp) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))) - .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 251, 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( @@ -194,19 +206,84 @@ public class cameraForm extends javax.swing.JFrame { .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.BASELINE) - .addComponent(jButton1) - .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jButton3)) + .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(); }// //GEN-END:initComponents - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed - // TODO add your handling code here: - }//GEN-LAST:event_jButton2ActionPerformed + 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 @@ -244,18 +321,18 @@ public class cameraForm extends javax.swing.JFrame { }}); } - public void onCreate() throws Exception{ + 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; - ServerSocket server = new ServerSocket(8485); - System.out.println("wait for connection on port 8080"); + server = new ServerSocket(port); + System.out.println("wait for connection on port " + port); boolean run = true; - Socket client = server.accept(); - System.out.println("got connection on port 8080"); + client = server.accept(); + System.out.println("got connection on port " + port); BufferedImage image = null; byte[] imageByte; int null_reps = 0; @@ -306,7 +383,7 @@ public class cameraForm extends javax.swing.JFrame { } server.close(); client.close(); - JOptionPane.showMessageDialog(this, "Ambulance socket server down!"); + JOptionPane.showMessageDialog(this, name +" socket server down!"); } @@ -324,6 +401,8 @@ public class cameraForm extends javax.swing.JFrame { } // 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; @@ -331,9 +410,7 @@ public class cameraForm extends javax.swing.JFrame { private javax.swing.JLabel fan_rpm; private javax.swing.JLabel gpu_temp; private javax.swing.JLabel gpu_usage; - private javax.swing.JButton jButton1; - private javax.swing.JButton jButton2; - private javax.swing.JButton jButton3; + private javax.swing.JButton intersection_button; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3;