count_fish.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import time
  2. import numpy as np
  3. from util.hard_disk_storage import HardDiskStorage
  4. from datetime import datetime
  5. hard_disk_db = HardDiskStorage()
  6. def count_fish(data, cage_length):
  7. if len(data) <= 5:
  8. return 0
  9. q1 = time.time()
  10. median_value = np.median(data)
  11. std_dev = np.std(data)
  12. threshold_max = median_value + 3 * std_dev
  13. threshold_min = median_value - 3 * std_dev
  14. if threshold_min < 0:
  15. threshold_min = 0
  16. data = [x for x in data if x > threshold_min and x < threshold_max]
  17. sorted_data = sorted(data, reverse=True)
  18. threshold_max = np.max(sorted_data)
  19. # print('cage_length:', cage_length, type(cage_length))
  20. if cage_length == 100:
  21. # 100
  22. threshold_70 = sorted_data[int(len(sorted_data) * 7 / 10)]
  23. else:
  24. # 160
  25. threshold_70 = sorted_data[len(sorted_data) - 1]
  26. finall_data = [d for d in data if d <= threshold_max and d >= threshold_70]
  27. mean_data = np.mean(finall_data)
  28. q2 = time.time()
  29. return int(mean_data)
  30. if __name__ == '__main__':
  31. print(str(datetime.now()), '----------------------------')
  32. data_list = []
  33. sql = "select * from measurement_data order by id desc limit 1;"
  34. # sql = "select * from measurement_data where uuid = 'pTydWY2r';"
  35. res = hard_disk_db.get_all(sql)
  36. if not res:
  37. print('当前无数据')
  38. exit()
  39. res = res[0]
  40. # res: {'id': 8, 'uuid': 'CYrNJcco', 'time_begin': datetime.datetime(2023, 10, 28, 15, 5, 2),
  41. # 'time_end': datetime.datetime(2023, 10, 28, 15, 22, 46), 'cage_length': 100, 'fish_weight': 1.5, 'cage_number': 401, 'fish_count': 0}
  42. print('箱号:', res['cage_number'], '周长:', res['cage_length'])
  43. uuid, time_end, fish_count = res['uuid'], res['time_end'], res['fish_count']
  44. sql1 = f"SELECT COUNT(uuid) as num FROM package_data WHERE uuid='{uuid}';"
  45. res1 = hard_disk_db.get_all(sql1)
  46. if not res1:
  47. print('未采集到数据!')
  48. exit()
  49. num = res1[0]['num']
  50. print('当前共采集数据:', num, '组')
  51. if num >= 288 and time_end is None:
  52. print('自动结束')
  53. sql2 = "update measurement_data set time_end=%s where uuid = %s;"
  54. var2 = (str(datetime.now()), uuid)
  55. hard_disk_db.execute_sql(sql2, var2)
  56. exit()
  57. if not time_end:
  58. print('采集评估中!')
  59. hard_disk_db.execute_sql("update measurement_data set note = '评估中!' where uuid = %s;", (uuid,))
  60. exit()
  61. print('已经结束采集!')
  62. sql3 = "select fish_count from package_data where uuid = %s and is_processed=1 AND is_abnormal=0;"
  63. var3 = (uuid,)
  64. res3 = hard_disk_db.execute_sql(sql3, var3)
  65. if len(res3) > 10:
  66. print('采集数据:', len(res3), '组')
  67. for i in res3:
  68. if i['fish_count']:
  69. data_list.append(int(i['fish_count']))
  70. else:
  71. print(i['fish_count'])
  72. fish_total = count_fish(data_list, res['cage_length'])
  73. print('尾数:', fish_total)
  74. sql4 = "update measurement_data set fish_count = %s where uuid = %s;"
  75. var4 = (fish_total, uuid,)
  76. hard_disk_db.execute_sql(sql4, var4)
  77. else:
  78. hard_disk_db.execute_sql("update measurement_data set note = '采集有效数据不足!' where uuid = %s;", (uuid,))
  79. print('采集有效数据小于10组:', len(res3), '组')