點擊下面卡片關注“AI算法與圖像處理”,選擇加"星標"或“置頂”
重磅乾貨,第一時間送達
讓我們編碼!
kwh_pln_sensor.py
from sqlalchemy import create_engine
import cv2
import numpy as np
import pandas as pd
import datetime
import time
-
sqlalchemy是連接數據庫(對象關係映射器)的助手,在本例中爲PostgreSQL -
cv2(開源的計算機視覺庫,或稱爲OpenCV):這個python庫可以幫助從相機中過濾出像素顏色,用於過濾紅色 -
pandas使創建矩陣列和行以將數據保存到數據庫變得更容易 -
datetime和time,管理日期和時間格式的庫
pip install opencv-python
pip install sqlalchemy
pip install pandas
conn =
create_engine("postgresql+psycopg2://root:12345678@localhost:5432/plnstats").connect()
cap = cv2.VideoCapture(0)
(ret, frame) = cap.read()
while True:
(ret, frame) = cap.read()
size = frame.size
image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
將原始幀轉換爲RGB顏色,image= cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower = np.array([168, 25, 0])
upper = np.array([179, 255, 255])
mask = cv2.inRange(image, lower, upper)
no_red = cv2.countNonZero(mask)
frac_red = np.divide(float(no_red), size)
percent_red = np.multiply((float(frac_red)), 100)
if (percent_red >= 10.0):
data_capture = {
'color_percentage': percent_red,
'created_on': datetime.datetime.now()
}
df = pd.DataFrame(columns=['color_percentage','created_on'],data=data_capture,index=[0])
df.to_sql('home_pln_kwh_sensor', schema='public', con=conn, if_exists='append',index=False)
from sqlalchemy import create_engine
import cv2
import numpy as np
import pandas as pd
import datetime
import time
conn = create_engine("postgresql+psycopg2://root:12345678@localhost:5432/plnstats").connect()
cap = cv2.VideoCapture(0)
(ret, frame) = cap.read()
capture_status = True
while capture_status:
(ret, frame) = cap.read()
size = frame.size
image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower = np.array([168, 25, 0])
upper = np.array([179, 255, 255])
mask = cv2.inRange(image, lower, upper)
no_red = cv2.countNonZero(mask)
frac_red = np.divide(float(no_red), size)
percent_red = np.multiply((float(frac_red)), 100)
print(percent_red)
if (percent_red >= 10.0): #estimated from the red colour percentage impulse LED lamp
data_capture = {
'color_percentage': percent_red,
'created_on': datetime.datetime.now()
}
df = pd.DataFrame(columns=['color_percentage','created_on'],data=data_capture,index=[0])
df.to_sql('home_pln_kwh_sensor', schema='public', con=conn, if_exists='append',index=False)
cap.release()
cv2.destroyAllWindows()
kwh_pln_calculate.py
import pandas as pd
from datetime import datetime,timedelta
from sqlalchemy import create_engine
IMPULSE_KWH = 1000
RUPIAH_PER_KWH = 1444.70
now_date = datetime.now().strftime('%Y-%m-%d')
yesterday_date = datetime.now() - timedelta(days=1)
yesterday_date = yesterday_date.strftime('%Y-%m-%d')
conn = create_engine("postgresql+psycopg2://root:12345678@localhost:5432/plnstats").connect()
df = pd.read_sql(sql='SELECT color_percentage, created_on from public.home_pln_kwh_sensor where created_on >= \'{yesterday} 00:00:00\' and created_on < \'{currdate} 00:00:00\''.format(yesterday=yesterday_date,currdate=now_date),con=conn)
df['created_on'] = pd.to_datetime(df.created_on)
df['created_on'] = df['created_on'].dt.strftime('%Y-%m-%d %H:%M:%S')
df = df.drop_duplicates(subset=['created_on'])
df['created_on'] = pd.to_datetime(df.created_on)
df['hour'] = df['created_on'].dt.strftime('%H')
df['day'] = df['created_on'].dt.strftime('%Y-%m-%d')
list_day = list(df['day'].unique())
df_summary = pd.DataFrame(columns=['day','hour','sum_impulse','charges_amount'])
for dday in list_day:
list_hour = list(df[df['day'] == str(dday)]['hour'].unique())
for y in list_hour:
wSumKWH = df[(df['day'] == str(dday)) & (df['hour'] == str(y))]
df_summary = df_summary.append({'hour':y,'day':dday,'sum_impulse':wSumKWH.groupby('hour').count()['day'][0],'charges_amount':(wSumKWH.groupby('hour').count()['day'][0])*RUPIAH_PER_KWH/IMPULSE_KWH},ignore_index=True)
df_summary.to_sql('home_pln_kwh_hour', schema='public', con=conn, if_exists='append', index=False)
credit_summary = df_summary[df_summary['day'] == yesterday_date].groupby('day').sum()
credit_summary.to_sql('home_pln_kwh_summary', schema='public', con=conn, if_exists='append', index=True)
import pandas as pd
from datetime import datetime,timedelta
from sqlalchemy import create_engine
IMPULSE_KWH = 1000 #depends on prepaid meter
RUPIAH_PER_KWH = 1444.70 # R-1/TR 1.301 – 2.200 VA
def run_cron():
now_date = datetime.now().strftime('%Y-%m-%d')
yesterday_date = datetime.now() - timedelta(days=1)
yesterday_date = yesterday_date.strftime('%Y-%m-%d')
conn = create_engine("postgresql+psycopg2://root:12345678@localhost:5432/plnstats").connect()
df = pd.read_sql(sql='SELECT color_percentage, created_on from public.home_pln_kwh_sensor where created_on >= \'{yesterday} 00:00:00\' and created_on < \'{currdate} 00:00:00\''.format(yesterday=yesterday_date,currdate=now_date),con=conn)
df['created_on'] = pd.to_datetime(df.created_on)
df['created_on'] = df['created_on'].dt.strftime('%Y-%m-%d %H:%M:%S')
df = df.drop_duplicates(subset=['created_on'])
df['created_on'] = pd.to_datetime(df.created_on)
df['hour'] = df['created_on'].dt.strftime('%H')
df['day'] = df['created_on'].dt.strftime('%Y-%m-%d')
list_day = list(df['day'].unique())
df_summary = pd.DataFrame(columns=['day','hour','sum_impulse','charges_amount'])
#DETAIL PER HOUR
for dday in list_day:
list_hour = list(df[df['day'] == str(dday)]['hour'].unique())
for y in list_hour:
wSumKWH = df[(df['day'] == str(dday)) & (df['hour'] == str(y))]
df_summary = df_summary.append({'hour':y,'day':dday,'sum_impulse':wSumKWH.groupby('hour').count()['day'][0],'charges_amount':(wSumKWH.groupby('hour').count()['day'][0])*RUPIAH_PER_KWH/IMPULSE_KWH},ignore_index=True)
df_summary.to_sql('home_pln_kwh_hour', schema='public', con=conn, if_exists='append', index=False)
credit_summary = df_summary[df_summary['day'] == yesterday_date].groupby('day').sum()
credit_summary.to_sql('home_pln_kwh_summary', schema='public', con=conn, if_exists='append', index=True)
if __name__ == '__main__':
run_cron()
個人微信(如果沒有備註不拉羣!)
請註明:
地區+學校/企業+研究方向+暱稱
下載1:何愷明頂會分享
在「AI算法與圖像處理」公衆號後臺回覆:何愷明,即可下載。總共有6份PDF,涉及 ResNet、Mask RCNN等經典工作的總結分析
下載2:終身受益的編程指南:Google編程風格指南
在「AI算法與圖像處理」公衆號後臺回覆:c++,即可下載。歷經十年考驗,最權威的編程規範!
下載3 CVPR2021
在「AI算法與圖像處理」公衆號後臺回覆:
CVPR
,即可下載1467篇CVPR 2020論文 和 CVPR 2021 最新論文
點亮 ,告訴大家你也在看
本文分享自微信公衆號 - AI算法與圖像處理(AI_study)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。