TerrenceDai commited on
Commit
a111e26
·
1 Parent(s): 2eeadab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -43
app.py CHANGED
@@ -15,55 +15,52 @@ def predict(file):
15
  try:
16
  with open(file.name, "r") as f:
17
  data = json.load(f)
18
-
19
- # Process the data (example: return the file content length)
20
- return f"File successfully processed. Content: {len(data)} items."
21
- except Exception as e:
22
- return f"Error processing file: {str(e)}"
23
-
24
- # 确保 JSON 格式正确
25
- channels = ["AccelX", "AccelY", "AccelZ", "GyroX", "GyroY", "GyroZ"]
26
- # if not all(channel in data for channel in channels):
27
- # return {"error": "JSON 文件格式错误,缺少必要通道数据。"}
28
 
29
- # 转换为 NumPy 数组,形状为 (6, sequence_length)
30
- imu_data = np.array([data[channel] for channel in channels])
 
 
31
 
32
- #Process data
33
- len_data = imu_data.shape[1]
34
- window_size = 30
35
- overlap = 15
36
- step = window_size - overlap
37
- predictions = [0,0,0,0]
38
-
39
 
40
- for start in range(0,len_data,step):
41
- end = start + window_size
42
- if end > len_data:
43
- # Zero padding to last window
44
- window = np.pad(imu_data[:, start:], ((0, 0), (0, end - len_data)), mode='constant')
45
- else:
46
- window = imu_data[:, start:end]
47
 
48
- input_tensor = torch.tensor(window, dtype=torch.float32).unsqueeze(0)
49
 
50
- with torch.no_grad():
51
- output = model(input_tensor)
52
- probabilities = torch.softmax(output, dim=-1).cpu().numpy()
53
- predicted_class = np.argmax(probabilities)
54
- #print(probabilities)
55
- if np.max(probabilities) > 0.70: #Threshold
56
- predictions[predicted_class] += 1
57
- #取眾數
58
- max_count = max(predictions)
59
- predictions = [x if x == max_count else 0 for x in predictions]
60
 
61
- #Result
62
- result = (
63
- f"bicep: {predictions[0]} | abs: {predictions[1]} | "
64
- f"chess: {predictions[2]} | legs: {predictions[3]}"
65
- )
66
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # 定义 Gradio 界面
69
  iface = gr.Interface(
 
15
  try:
16
  with open(file.name, "r") as f:
17
  data = json.load(f)
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # 确保 JSON 格式正确
20
+ channels = ["AccelX", "AccelY", "AccelZ", "GyroX", "GyroY", "GyroZ"]
21
+ # if not all(channel in data for channel in channels):
22
+ # return {"error": "JSON 文件格式错误,缺少必要通道数据。"}
23
 
24
+ # 转换为 NumPy 数组,形状为 (6, sequence_length)
25
+ imu_data = np.array([data[channel] for channel in channels])
 
 
 
 
 
26
 
27
+ #Process data
28
+ len_data = imu_data.shape[1]
29
+ window_size = 30
30
+ overlap = 15
31
+ step = window_size - overlap
32
+ predictions = [0,0,0,0]
 
33
 
 
34
 
35
+ for start in range(0,len_data,step):
36
+ end = start + window_size
37
+ if end > len_data:
38
+ # Zero padding to last window
39
+ window = np.pad(imu_data[:, start:], ((0, 0), (0, end - len_data)), mode='constant')
40
+ else:
41
+ window = imu_data[:, start:end]
42
+
43
+ input_tensor = torch.tensor(window, dtype=torch.float32).unsqueeze(0)
 
44
 
45
+ with torch.no_grad():
46
+ output = model(input_tensor)
47
+ probabilities = torch.softmax(output, dim=-1).cpu().numpy()
48
+ predicted_class = np.argmax(probabilities)
49
+ #print(probabilities)
50
+ if np.max(probabilities) > 0.70: #Threshold
51
+ predictions[predicted_class] += 1
52
+ #取眾數
53
+ max_count = max(predictions)
54
+ predictions = [x if x == max_count else 0 for x in predictions]
55
+
56
+ #Result
57
+ result = (
58
+ f"bicep: {predictions[0]} | abs: {predictions[1]} | "
59
+ f"chess: {predictions[2]} | legs: {predictions[3]}"
60
+ )
61
+ return result
62
+ except Exception as e:
63
+ return f"Error processing file: {str(e)}"
64
 
65
  # 定义 Gradio 界面
66
  iface = gr.Interface(