123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import time
- import numpy as np
- from util.hard_disk_storage import HardDiskStorage
- from datetime import datetime
- hard_disk_db = HardDiskStorage()
- def count_fish(data, cage_length):
- if len(data) <= 5:
- return 0
- q1 = time.time()
- median_value = np.median(data)
- std_dev = np.std(data)
- threshold_max = median_value + 3 * std_dev
- threshold_min = median_value - 3 * std_dev
- if threshold_min < 0:
- threshold_min = 0
- data = [x for x in data if x > threshold_min and x < threshold_max]
- sorted_data = sorted(data, reverse=True)
- threshold_max = np.max(sorted_data)
- # print('cage_length:', cage_length, type(cage_length))
- if cage_length == 100:
- # 100
- threshold_70 = sorted_data[int(len(sorted_data) * 7 / 10)]
- else:
- # 160
- threshold_70 = sorted_data[len(sorted_data) - 1]
- finall_data = [d for d in data if d <= threshold_max and d >= threshold_70]
- mean_data = np.mean(finall_data)
- q2 = time.time()
- return int(mean_data)
- if __name__ == '__main__':
- print(str(datetime.now()), '----------------------------')
- data_list = []
- sql = "select * from measurement_data order by id desc limit 1;"
- # sql = "select * from measurement_data where uuid = 'pTydWY2r';"
- res = hard_disk_db.get_all(sql)
- if not res:
- print('当前无数据')
- exit()
- res = res[0]
- # res: {'id': 8, 'uuid': 'CYrNJcco', 'time_begin': datetime.datetime(2023, 10, 28, 15, 5, 2),
- # 'time_end': datetime.datetime(2023, 10, 28, 15, 22, 46), 'cage_length': 100, 'fish_weight': 1.5, 'cage_number': 401, 'fish_count': 0}
- print('箱号:', res['cage_number'], '周长:', res['cage_length'])
- uuid, time_end, fish_count = res['uuid'], res['time_end'], res['fish_count']
- sql1 = f"SELECT COUNT(uuid) as num FROM package_data WHERE uuid='{uuid}';"
- res1 = hard_disk_db.get_all(sql1)
- if not res1:
- print('未采集到数据!')
- exit()
- num = res1[0]['num']
- print('当前共采集数据:', num, '组')
- if num >= 288 and time_end is None:
- print('自动结束')
- sql2 = "update measurement_data set time_end=%s where uuid = %s;"
- var2 = (str(datetime.now()), uuid)
- hard_disk_db.execute_sql(sql2, var2)
- exit()
- if not time_end:
- print('采集评估中!')
- hard_disk_db.execute_sql("update measurement_data set note = '评估中!' where uuid = %s;", (uuid,))
- exit()
- print('已经结束采集!')
- sql3 = "select fish_count from package_data where uuid = %s and is_processed=1 AND is_abnormal=0;"
- var3 = (uuid,)
- res3 = hard_disk_db.execute_sql(sql3, var3)
- if len(res3) > 10:
- print('采集数据:', len(res3), '组')
- for i in res3:
- if i['fish_count']:
- data_list.append(int(i['fish_count']))
- else:
- print(i['fish_count'])
- fish_total = count_fish(data_list, res['cage_length'])
- print('尾数:', fish_total)
- sql4 = "update measurement_data set fish_count = %s where uuid = %s;"
- var4 = (fish_total, uuid,)
- hard_disk_db.execute_sql(sql4, var4)
- else:
- hard_disk_db.execute_sql("update measurement_data set note = '采集有效数据不足!' where uuid = %s;", (uuid,))
- print('采集有效数据小于10组:', len(res3), '组')
|