강수빈

코드 추가

import smbus #import SMBus module of I2C
from time import sleep #import
import time
import threading
#some MPU6050 Registers and their Address
PWR_MGMT_1 = 0x6B
SMPLRT_DIV = 0x19
CONFIG = 0x1A
GYRO_CONFIG = 0x1B
INT_ENABLE = 0x38
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F
GYRO_XOUT_H = 0x43
GYRO_YOUT_H = 0x45
GYRO_ZOUT_H = 0x47
bus = smbus.SMBus(1) # or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x68 # MPU6050 device address
def MPU_Init():
#write to sample rate register
bus.write_byte_data(Device_Address, SMPLRT_DIV, 7)
#Write to power management register
bus.write_byte_data(Device_Address, PWR_MGMT_1, 1)
#Write to Configuration register
bus.write_byte_data(Device_Address, CONFIG, 0)
#Write to Gyro configuration register
bus.write_byte_data(Device_Address, GYRO_CONFIG, 24)
#Write to interrupt enable register
bus.write_byte_data(Device_Address, INT_ENABLE, 1)
def read_raw_data(addr):
#Accelero and Gyro value are 16-bit
high = bus.read_byte_data(Device_Address, addr)
low = bus.read_byte_data(Device_Address, addr+1)
#concatenate higher and lower value
value = ((high << 8) | low)
#to get signed value from mpu6050
if(value > 32768):
value = value - 65536
return value
def start_record(data):
MPU_Init()
t = threading.currentThread()
print (" Reading Data of Gyroscope and Accelerometer")
start_time=time.time()
while getattr(t, "do_run", True):
acc_x = read_raw_data(ACCEL_XOUT_H)
acc_y = read_raw_data(ACCEL_YOUT_H)
acc_z = read_raw_data(ACCEL_ZOUT_H)
value=abs(acc_x+acc_y+acc_z)/10
data.append(value)
print(value)
if(time.time()>start_time+10):
break
from flask import Flask, send_file,request,send_from_directory, render_template
import matlab.engine
from werkzeug.utils import secure_filename
import os
import json
app = Flask(__name__)
@app.route('/', methods=['GET'])
def maiddn():
return render_template('view.html')
@app.route('/upload', methods=['POST'])
def upload_file():
params = json.loads(request.get_data(), encoding='utf-8')
data=params['datas']
print(len(data))
make_wav_file(data)
return send_file("test.wav", attachment_filename='test.wav', as_attachment=True)
def make_wav_file(data):
print('convert is loading')
eng = matlab.engine.start_matlab()
l1=[]
for t in data:
try:
l1.append(int(t))
except:
print(t)
l2 = [n/10000 for n in l1]
data = matlab.double(l2)
filename = 'test.wav'
eng.audiowrite(filename, data, 1000, nargout=0)
eng.quit()
@app.route('/combine', methods=['POST'])
def combineMp4Wav():
print(request.files)
if 'Video' not in request.files:
print("error")
return "error"
file = request.files['Video']
print(file)
file.save("video.mp4")
os.system("ffmpeg -i video.mp4 -i test.wav -c:v copy -c:a aac -strict experimental -vcodec libx264 -map 0:v:0 -map 1:a:0 -y output.mp4")
return "Success"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
import { WaveGroup } from "./wavegroup.js";
class App{
constructor(){
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
document.body.appendChild(this.canvas);
this.waveGroup = new WaveGroup();
window.addEventListener('resize', this.resize.bind(this), false);
this.resize();
requestAnimationFrame(this.animate.bind(this));
}
resize() {
this.stageWidth = document.body.clientWidth;
this.stageHeight = document.body.clientHeight;
this.canvas.width = this.stageWidth * 2;
this.canvas.height = this.stageHeight * 2;
this.ctx.scale(2,2);
this.waveGroup.resize(this.stageWidth,this.stageHeight);
}
animate(t){
this.ctx.clearRect(0,0,this.stageWidth,this.stageHeight);
this.waveGroup.draw(this.ctx);
requestAnimationFrame(this.animate.bind(this));
}
}
window.onload = () =>{
new App();
}
\ No newline at end of file
No preview for this file type
export class Point {
constructor(index, x, y){
this.x = x;
this.y = y;
this.fixedY = y;
this.speed = 0.03;
this.cur = index;
this.max = Math.random() * 100 +200;
}
update() {
this.cur += this.speed;
this.y = this.fixedY + (Math.sin(this.cur) * this.max);
}
}
\ No newline at end of file
*{
user-select: none;
-ms-user-select: none;
outline: 0;
margin: 0;
padding: 0;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
html {
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
overflow: hidden;
background-color: #ffffff;
}
canvas {
width: 100%;
height: 100%;
}
button {
width:100px;
background-color: #f8585b;
border: none;
color:#fff;
padding: 15px 0;
border-radius:10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
margin: 4px;
cursor: pointer;
}
import {
Point
} from './point.js'
export class Wave {
constructor(index, totalPoints, color) {
this.index = index;
this.totalPoints = totalPoints;
this.color = color;
this.points = [];
}
resize(stageWidth, stageHeight){
this.stageWidth = stageWidth;
this.stageHeight = stageHeight;
this.centerX = stageWidth /2;
this.centerY = stageHeight /2;
this.pointGap = this.stageWidth/ (this.totalPoints - 1);
this.init();
}
init(){
this.points = [];
for (let i = 0; i < this.totalPoints; i++){
const point = new Point(
this.index +i,
this.pointGap * i,
this.centerY,
);
this.points[i] = point;
}
}
draw(ctx) {
ctx.beginPath();
ctx.fillStyle = this.color;
let prevX = this.points[0].x;
let prevY = this.points[0].y;
ctx.moveTo(prevX,prevY);
for(let i = 1; i < this.totalPoints; i++){
if (i < this.totalPoints - 1){
this.points[i].update();
}
const cx = (prevX + this.points[i].x) / 2;
const cy = (prevY + this.points[i].y) / 2;
ctx.quadraticCurveTo(prevX, prevY, cx,cy);
prevX = this.points[i].x;
prevY = this.points[i].y;
}
ctx.lineTo(prevX,prevY);
ctx.lineTo(this.stageWidth,this.stageHeight);
ctx.lineTo(this.points[0].x,this.stageHeight);
ctx.fill();
ctx.closePath();
}
}
\ No newline at end of file
import{
Wave
} from './wave.js'
export class WaveGroup {
constructor(){
this.totalWaves = 3;
this.totalPoints = 6;
this.color = ['rgba(255,199,235,0.4)','rgba(255,146,199,0.4)','rgba(0,87,158,0.4)'];
this.waves = [];
for (let i = 0; i < this.totalWaves; i++){
const wave = new Wave(
i,
this.totalPoints,
this.color[i]
);
this.waves[i] = wave;
}
}
resize(stageWidth, stageHeight){
for (let i =0; i < this.totalWaves; i++){
const wave = this.waves[i];
wave.resize(stageWidth,stageHeight);
}
}
draw(ctx){
for (let i =0; i < this.totalWaves; i++){
const wave = this.waves[i];
wave.draw(ctx);
}
}
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>Haptic Recording</title>
<link rel="stylesheet" href="{{url_for('static', filename = 'style.css')}}">
<style>
#start {
width:100px;
background-color: #f8585b;
border: none;
color:#fff;
padding: 15px 0;
border-radius:10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
margin: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<div style="text-align: center; padding-top: 10%;">
<h1>Haptic Data Recording</h1>
<p style="margin-top: 2%;">Subeen Kang & Jinhyeong Park</p>
<form method ="post" action="/record">
<button style="margin-top: 10%;" type="button" id="start" > START </button>
</form>
</div>
<script type="module" src="{{url_for('static', filename = 'app.js')}}"></script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>Haptic Recording</title>
<link rel="stylesheet" href="{{url_for('static', filename = 'style.css')}}">
<style>
#stop {
width:100px;
background-color: #f8585b;
border: none;
color:#fff;
padding: 15px 0;
border-radius:10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
margin: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<div style="text-align: center; padding-top: 10%;">
<h1>Converting...</h1>
<h1 style="margin-top: 2%; color:#f8585b;"id="clock">00:00</h1>
<form method ="post" action="/stop">
<button style="margin-top: 10%;" id="stop" type="button"> STOP </button>
</form>
</div>
<script type="module" src="{{url_for('static', filename = 'app.js')}}"></script>
</body>
<script>
var clockTarget = document.getElementById("clock");
var seconds = 0;
var miliseconds = 0;
function clock() {
clockTarget .innerText = `${seconds < 10 ? `0${seconds }`: seconds }:${miliseconds < 10 ? `0${miliseconds }` :miliseconds}`;
miliseconds = miliseconds +1;
if(miliseconds >99){
seconds = seconds +1;
miliseconds = miliseconds %100;
}
}
var inter;
function init() {
clock();
inter = setInterval(clock, 10);
}
function stopit(){
clearInterval(inter);
}
document.getElementById('stop').addEventListener('click', stopit);
init();
</script>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<body>
<div style="text-align: center;">
<video controls width="900">
<source src="{{url_for('static', filename = 'output.mp4')}}" type="video/mp4"></source>
이 문장은 여러분의 브라우저가 video 태그를 지원하지 않을 때 화면에 표시됩니다!
</video>
</div>
</body>
</html>
\ No newline at end of file
No preview for this file type
No preview for this file type
from flask import Flask, send_file,request,url_for,redirect, render_template
import os
import json
import acc_test_2
import threading
import multiprocessing
import requests
import pygame
import time
app = Flask(__name__)
global data
global th
data=[]
th = threading.Thread(target=acc_test_2.start_record,args=(data,))
th.setDaemon(True)
@app.route('/')
def upload_main():
return render_template('index.html')
@app.route('/record', methods=['GET','POST'])
def submit():
global th
global data
print(threading.enumerate())
if not th.is_alive():
print('dead')
data=[]
th = threading.Thread(target=acc_test_2.start_record,args=(data,))
th.start()
return render_template('record.html')
@app.route('/stop', methods=['GET','POST'])
def stop():
th.do_run = False
th.join()
print("data length is " + str(len(data)))
r = upload(data)
return (r.content, r.status_code, r.headers.items())
def upload(datas):
params={'datas':datas}
url='http://192.168.0.25:80/upload'
return requests.post(url,data =json.dumps(params))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
\ No newline at end of file