김민석

final

1 +{
2 + "cells": [
3 + {
4 + "cell_type": "code",
5 + "execution_count": 1,
6 + "metadata": {},
7 + "outputs": [],
8 + "source": [
9 + "import socket \n",
10 + "import select \n",
11 + "import os "
12 + ]
13 + },
14 + {
15 + "cell_type": "code",
16 + "execution_count": null,
17 + "metadata": {},
18 + "outputs": [],
19 + "source": [
20 + "def removeAllFile(filePath):\n",
21 + " if os.path.exists(filePath):\n",
22 + " for file in os.scandir(filePath):\n",
23 + " os.remove(file.path)\n",
24 + " return 'Remove All File'\n",
25 + " else:\n",
26 + " return 'Directory Not Found'\n",
27 + "\n",
28 + "print(removeAllFile('/home/pi/capdesign/frames'))\n",
29 + "address = ('192.168.51.1', 8083)\n",
30 + "sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n",
31 + "sock.bind(address)\n",
32 + "\n",
33 + "while True:\n",
34 + " data, addr = sock.recvfrom(1024)\n",
35 + " if data:\n",
36 + " data = data.decode()\n",
37 + " print(\"filename\", data)\n",
38 + " file_name = data.strip()\n",
39 + " \n",
40 + " f = open('frames/{}'.format(file_name), 'wb')\n",
41 + " \n",
42 + " while True:\n",
43 + " ready = select.select([sock], [], [], 3)\n",
44 + " if ready[0]:\n",
45 + " data, addr = sock.recvfrom(1024)\n",
46 + " if(data == \"FINISH\".encode('utf-8')):\n",
47 + " f.close()\n",
48 + " break\n",
49 + " else:\n",
50 + " f.write(data) "
51 + ]
52 + }
53 + ],
54 + "metadata": {
55 + "kernelspec": {
56 + "display_name": "Python 3",
57 + "language": "python",
58 + "name": "python3"
59 + },
60 + "language_info": {
61 + "codemirror_mode": {
62 + "name": "ipython",
63 + "version": 3
64 + },
65 + "file_extension": ".py",
66 + "mimetype": "text/x-python",
67 + "name": "python",
68 + "nbconvert_exporter": "python",
69 + "pygments_lexer": "ipython3",
70 + "version": "3.8.5"
71 + }
72 + },
73 + "nbformat": 4,
74 + "nbformat_minor": 4
75 +}
1 +{
2 + "CurrentProjectSetting": null
3 +}
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "ExpandedNodes": [
3 + ""
4 + ],
5 + "SelectedNode": "\\index.js",
6 + "PreviewInSolutionExplorer": false
7 +}
...\ No newline at end of file ...\ No newline at end of file
No preview for this file type
No preview for this file type
This diff could not be displayed because it is too large.
1 -from picamera import PiCamera
2 -from time import sleep
3 -import socket
4 -import cv2
5 -
6 -buf = 1024
7 -address = ('192.168.50.1', 8080)
8 -
9 -try:
10 - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
11 - print('Sender Socket Initialized')
12 -except:
13 - print("fail")
14 -
15 -#camera = PiCamera()
16 -vid = cv2.VideoCapture(-1)
17 -
18 -i = 0
19 -while(True):
20 - ret, frame = vid.read()
21 - cv2.imshow('frame', frame)
22 - cv2.imwrite('/home/pi/Desktop/frames/{}.jpg'.format(i), frame)
23 - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
24 - s.sendto('{}.jpg'.format(i).encode(), address)
25 - print('sending file_name to server....')
26 - f = open('/home/pi/Desktop/frames/{}.jpg'.format(i), 'rb')
27 - i += 1
28 - if(f):
29 - print("yes")
30 - data = f.read(buf)
31 - while(data):
32 - if(s.sendto(data,address)):
33 - print('sending...')
34 - data = f.read(buf)
35 - s.close()
36 - if cv2.waitKey(1) & 0xFF == ord('q'):
37 - break
38 -
39 -vid.release()
40 -cv2.destroyAllWindows()
...\ No newline at end of file ...\ No newline at end of file
...@@ -2,6 +2,11 @@ let express = require('express'); ...@@ -2,6 +2,11 @@ let express = require('express');
2 let app = express(); 2 let app = express();
3 let path = require('path'); 3 let path = require('path');
4 let fs = require('fs'); 4 let fs = require('fs');
5 +let frameCount = 0;
6 +const dir = './frames'
7 +
8 +app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
9 +app.use('/frames', express.static(__dirname + '/frames'));
5 10
6 app.listen(3000, function(){ 11 app.listen(3000, function(){
7 console.log("hosting server start"); 12 console.log("hosting server start");
...@@ -13,11 +18,12 @@ app.get('/', function(req, res){ ...@@ -13,11 +18,12 @@ app.get('/', function(req, res){
13 }); 18 });
14 19
15 app.get('/frames', function(req, res){ 20 app.get('/frames', function(req, res){
16 - fs.readFile('./frames/1.jpg', function(error, data){ 21 + fs.readdir(dir, (err, files)=>{
22 + frameCount = files.length;
23 + })
24 +
25 + fs.readFile('./frames/' + String(frameCount-1)+'.jpg', function(error, data){
17 res.writeHead(200, {'Content-Type' : 'text/html'}); 26 res.writeHead(200, {'Content-Type' : 'text/html'});
18 res.end(data); 27 res.end(data);
19 }) 28 })
20 }); 29 });
21 -
22 -app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
23 -app.use('/frames', express.static(__dirname + '/frames'));
......
This diff could not be displayed because it is too large.
This file is too large to display.
This file is too large to display.
No preview for this file type
This file is too large to display.
1 +from picamera import PiCamera
2 +from time import sleep
3 +import socket
4 +import cv2
5 +
6 +capture = cv2.VideoCapture(0)
7 +capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
8 +capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
9 +
10 +i = 0
11 +finish_msg = "FINISH"
12 +
13 +buf = 1024
14 +address = ('192.168.50.1', 8080)
15 +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
16 +
17 +while cv2.waitKey(33) < 0:
18 + ret, frame = capture.read()
19 + # cv2.imshow('frame', frame) # img를 보여주는 곳
20 + cv2.imwrite('frames/{}.jpg'.format(i), frame)
21 +
22 + try:
23 + file_name = str(i) + '.jpg'
24 + sock.sendto(file_name.encode('utf-8'), address)
25 +
26 + f = open('frames/{}'.format(file_name), 'rb')
27 + data = f.read(buf)
28 +
29 + print(file_name, "Sending....")
30 + while(data):
31 + sock.sendto(data, address)
32 + data = f.read(buf)
33 +
34 + if(sock.sendto(finish_msg.endcode('utf-8'), address)):
35 + print("Finish message send")
36 +
37 + print(file_name, "send finish")
38 + f.close()
39 + except:
40 + print("error")
41 + i+=1
42 +
43 +capture.release()
44 +cv2.destroyAllWindows()
...\ No newline at end of file ...\ No newline at end of file
1 +import socket
2 +import select
3 +import os
4 +
5 +def removeAllFile(filePath):
6 + if os.path.exists(filePath):
7 + for file in os.scandir(filePath):
8 + os.remove(file.path)
9 + return 'Remove All File'
10 + else:
11 + return 'Directory Not Found'
12 +
13 +print(removeAllFile('/home/pi/capdesign/frames'))
14 +
15 +address = ('192.168.50.1', 8080)
16 +
17 +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
18 +sock.bind(address)
19 +
20 +sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
21 +send_address = ('192.168.51.1', 8083)
22 +buf = 1024
23 +finish_msg = "FINISH"
24 +
25 +while True:
26 + data, addr = sock.recvfrom(1024)
27 + if data:
28 + print("filename", data)
29 + file_name = data.strip()
30 +
31 + f = open('frames/{}'.format(file_name), 'wb')
32 +
33 + while True:
34 + ready = select.select([sock], [], [], 3)
35 + if ready[0]:
36 + data, addr = sock.recvfrom(1024)
37 + if(data == "FINISH".encode('utf-8')):
38 + print("Receivin {} FINISH".format(file_name))
39 + f.close()
40 + break
41 + else:
42 + f.write(data)
43 +
44 + # send to uav2
45 + try:
46 + print(file_name)
47 + sock2.sendto(file_name.encode('utf-8'), send_address)
48 + print("file name sending success")
49 + f = open('frames/{}'.format(file_name), 'rb')
50 + data = f.read(1024)
51 +
52 + print(file_name, "Sending...")
53 + while(data):
54 + try:
55 + sock2.sendto(data, send_address)
56 + except:
57 + print("sock2 makes problem")
58 +
59 + try:
60 + data = f.read(1024)
61 + except:
62 + print("data read makes problem")
63 + print("data sending finished")
64 + if(sock2.sendto(finish_msg.encode('utf-8'), send_address)):
65 + print('Finish message send')
66 + print(file_name, "send finish")
67 + f.close()
68 + except:
69 + print("sending error")
70 +
71 +sock.close()
72 +sock2.close()
73 +
1 +{
2 + "cells": [
3 + {
4 + "cell_type": "code",
5 + "execution_count": 1,
6 + "metadata": {},
7 + "outputs": [],
8 + "source": [
9 + "import socket \n",
10 + "import select \n",
11 + "import os "
12 + ]
13 + },
14 + {
15 + "cell_type": "code",
16 + "execution_count": null,
17 + "metadata": {},
18 + "outputs": [],
19 + "source": [
20 + "def removeAllFile(filePath):\n",
21 + " if os.path.exists(filePath):\n",
22 + " for file in os.scandir(filePath):\n",
23 + " os.remove(file.path)\n",
24 + " return 'Remove All File'\n",
25 + " else:\n",
26 + " return 'Directory Not Found'\n",
27 + "\n",
28 + "print(removeAllFile('/home/pi/capdesign/frames'))\n",
29 + "address = ('192.168.51.1', 8083)\n",
30 + "sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n",
31 + "sock.bind(address)\n",
32 + "\n",
33 + "while True:\n",
34 + " data, addr = sock.recvfrom(1024)\n",
35 + " if data:\n",
36 + " data = data.decode()\n",
37 + " print(\"filename\", data)\n",
38 + " file_name = data.strip()\n",
39 + " \n",
40 + " f = open('frames/{}'.format(file_name), 'wb')\n",
41 + " \n",
42 + " while True:\n",
43 + " ready = select.select([sock], [], [], 3)\n",
44 + " if ready[0]:\n",
45 + " data, addr = sock.recvfrom(1024)\n",
46 + " if(data == \"FINISH\".encode('utf-8')):\n",
47 + " f.close()\n",
48 + " break\n",
49 + " else:\n",
50 + " f.write(data) 2"
51 + ]
52 + }
53 + ],
54 + "metadata": {
55 + "kernelspec": {
56 + "display_name": "Python 3",
57 + "language": "python",
58 + "name": "python3"
59 + },
60 + "language_info": {
61 + "codemirror_mode": {
62 + "name": "ipython",
63 + "version": 3
64 + },
65 + "file_extension": ".py",
66 + "mimetype": "text/x-python",
67 + "name": "python",
68 + "nbconvert_exporter": "python",
69 + "pygments_lexer": "ipython3",
70 + "version": "3.8.5"
71 + }
72 + },
73 + "nbformat": 4,
74 + "nbformat_minor": 4
75 +}
1 +#!/usr/bin/env python
2 +# coding: utf-8
3 +
4 +# In[1]:
5 +
6 +
1 import socket 7 import socket
2 import select 8 import select
9 +import os
3 10
4 -sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
5 -sock.bind(('192.168.50.1', 8080))
6 11
7 -address = ('192.168.50.1', 8080) 12 +# In[ ]:
13 +
14 +
15 +def removeAllFile(filePath):
16 + if os.path.exists(filePath):
17 + for file in os.scandir(filePath):
18 + os.remove(file.path)
19 + return 'Remove All File'
20 + else:
21 + return 'Directory Not Found'
22 +
23 +print(removeAllFile('/home/pi/capdesign/frames'))
24 +address = ('192.168.51.1', 8083)
25 +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
26 +sock.bind(address)
8 27
9 while True: 28 while True:
10 data, addr = sock.recvfrom(1024) 29 data, addr = sock.recvfrom(1024)
11 if data: 30 if data:
31 + data = data.decode()
12 print("filename", data) 32 print("filename", data)
13 file_name = data.strip() 33 file_name = data.strip()
14 34
...@@ -18,10 +38,9 @@ while True: ...@@ -18,10 +38,9 @@ while True:
18 ready = select.select([sock], [], [], 3) 38 ready = select.select([sock], [], [], 3)
19 if ready[0]: 39 if ready[0]:
20 data, addr = sock.recvfrom(1024) 40 data, addr = sock.recvfrom(1024)
21 - f.write(data) 41 + if(data == "FINISH".encode('utf-8')):
22 - else:
23 - print("Finish", file_name)
24 f.close() 42 f.close()
25 break 43 break
26 - sock.close() 44 + else:
45 + f.write(data)
27 46
......