diff --git a/BrainWave_Pred.py b/BrainWave_Pred.py new file mode 100644 index 0000000..23ce885 --- /dev/null +++ b/BrainWave_Pred.py @@ -0,0 +1,139 @@ +import numpy as np +from joblib import load +import os +# from __future__ import unicode_literals, print_function +from socket import socket, AF_INET, SOCK_DGRAM +from pythonosc import osc_message +import time + +HOST = '' +PORT = 8001 +class EEGPredictor: + def __init__(self, model_dir='trained_model'): + self.model, self.scaler = self._load_model(model_dir) + self.class_names = {0: 'neutral', 1: 'right', 2: 'left'} + self.feature_names = [ + 'alpha', 'beta', 'theta', 'delta', 'gamma', + 'alpha_beta_ratio', 'theta_beta_ratio', 'alpha_theta_ratio', + 'alpha_rel', 'beta_rel', 'theta_rel', 'delta_rel', 'gamma_rel', + 'alpha_log', 'beta_log', 'theta_log', 'delta_log', 'gamma_log' + ] + + def _load_model(self, model_dir): + model_path = os.path.join(model_dir, 'rf_model.joblib') + scaler_path = os.path.join(model_dir, 'scaler.joblib') + return load(model_path), load(scaler_path) + + def _create_features(self, basic_features): + """基本的な脳波データから追加の特徴量を生成""" + features = {} + + # 基本特徴量 + alpha, beta, theta, delta, gamma = basic_features + features.update({ + 'alpha': alpha, 'beta': beta, 'theta': theta, + 'delta': delta, 'gamma': gamma + }) + + # 比率の計算 + features['alpha_beta_ratio'] = alpha / beta if beta != 0 else 0 + features['theta_beta_ratio'] = theta / beta if beta != 0 else 0 + features['alpha_theta_ratio'] = alpha / theta if theta != 0 else 0 + + # 相対パワーの計算 + total_power = sum(basic_features) + if total_power != 0: + features.update({ + 'alpha_rel': alpha / total_power, + 'beta_rel': beta / total_power, + 'theta_rel': theta / total_power, + 'delta_rel': delta / total_power, + 'gamma_rel': gamma / total_power + }) + else: + features.update({ + 'alpha_rel': 0, 'beta_rel': 0, 'theta_rel': 0, + 'delta_rel': 0, 'gamma_rel': 0 + }) + + # 対数変換 + features.update({ + 'alpha_log': np.log1p(alpha) if alpha > 0 else 0, + 'beta_log': np.log1p(beta) if beta > 0 else 0, + 'theta_log': np.log1p(theta) if theta > 0 else 0, + 'delta_log': np.log1p(delta) if delta > 0 else 0, + 'gamma_log': np.log1p(gamma) if gamma > 0 else 0 + }) + + # 特徴量を正しい順序で並べ替え + return [features[name] for name in self.feature_names] + + def predict(self, eeg_data): + """脳波データから予測を行う""" + # 特徴量の生成 + features = self._create_features(eeg_data) + features = np.array(features).reshape(1, -1) + + # スケーリングと予測 + scaled_data = self.scaler.transform(features) + prediction = self.model.predict(scaled_data)[0] + probabilities = self.model.predict_proba(scaled_data)[0] + + # 結果の整形 + result = { + 'predicted_class': self.class_names[prediction], + 'confidence': float(probabilities[prediction]), + 'probabilities': { + self.class_names[i]: float(prob) + for i, prob in enumerate(probabilities) + } + } + + return result +def process_eeg_data(raw_eeg_data): + """ + 脳波データを処理して予測を行う + + Parameters: + raw_eeg_data: [alpha, beta, theta, delta, gamma]の形式の脳波データ + """ + predictor = EEGPredictor() + result = predictor.predict(raw_eeg_data) + + print(f"Predicted class: {result['predicted_class']}") + print(f"Confidence: {result['confidence']:.2f}") + print("Class probabilities:") + for class_name, prob in result['probabilities'].items(): + print(f" {class_name}: {prob:.2f}") + + return result + +def Convert_BrainWave(data): + msg = osc_message.OscMessage(data) + #print(msg.params) + types = msg.address + arguments = [] + if types == "/Attention": + arguments.append("Attention") + arguments.append(float(msg.params[0])) + + elif types == "/Meditation": + arguments.append("Meditation") + arguments.append(float(msg.params[0])) + + elif types == "/BandPower": + arguments.append("BandPower") + arguments += list(map(float, msg.params[0].split(";"))) + #print(map(float, msg.params[0].split(";"))) + return arguments +s = socket(AF_INET, SOCK_DGRAM) +s.bind((HOST, PORT)) +while True: + # 実際の脳波データをここに入れます + print("受信待ち") + data, address = s.recvfrom(1024) + received_data = Convert_BrainWave(data=data) + received_data = received_data[1:] + print(received_data) + result = process_eeg_data(received_data) + time.sleep(0.5) diff --git a/__pycache__/TaskManager.cpython-39.pyc b/__pycache__/TaskManager.cpython-39.pyc index 516a348..4a627af 100644 --- a/__pycache__/TaskManager.cpython-39.pyc +++ b/__pycache__/TaskManager.cpython-39.pyc Binary files differ diff --git a/csvfiles/nouhadata_Attention2024-12-19,11-24-40-242172.csv b/csvfiles/nouhadata_Attention2024-12-19,11-24-40-242172.csv new file mode 100644 index 0000000..4b683f7 --- /dev/null +++ b/csvfiles/nouhadata_Attention2024-12-19,11-24-40-242172.csv @@ -0,0 +1,27 @@ +attention,key +2.0987415313720703,1 +2.107482433319092,1 +1.9515609741210938,1 +1.9395179748535156,1 +2.604310989379883,1 +4.554898262023926,1 +6.518619537353516,1 +9.199201583862305,1 +12.295114517211914,1 +12.660309791564941,1 +12.981464385986328,1 +13.152748107910156,1 +13.247297286987305,1 +13.321399688720703,1 +13.970457077026367,1 +14.781240463256836,1 +15.143878936767578,1 +20.33324432373047,1 +22.145864486694336,1 +27.927885055541992,1 +32.14533233642578,1 +36.58976364135742,1 +39.6098747253418,1 +38.88203048706055,1 +36.006103515625,1 +34.713565826416016,1 diff --git a/csvfiles/nouhadata_BandPower2024-12-19,11-24-40-242172.csv b/csvfiles/nouhadata_BandPower2024-12-19,11-24-40-242172.csv new file mode 100644 index 0000000..863ced6 --- /dev/null +++ b/csvfiles/nouhadata_BandPower2024-12-19,11-24-40-242172.csv @@ -0,0 +1,128 @@ +alpha,beta,theta,delta,gamma,key +0.02793330232073587,0.0122924916666475,0.006963732050714714,0.03466245690303903,0.07027987026516042,1 +0.029118771868249075,0.011646405954480292,0.007599110162096893,0.033560510563700977,0.06897316139066828,1 +0.028780884479925106,0.011027296580463206,0.007717516513573689,0.033589845268774454,0.07173838681022972,1 +0.028530977179970756,0.0123858625732432,0.007605288181713947,0.03510128672113952,0.07038207481317563,1 +0.029664128883077664,0.012703763698338682,0.007443156696111306,0.03510340922300895,0.06923726948431266,1 +0.02968241110740592,0.012510712091228027,0.007506414068680202,0.03442947501131555,0.06992841020998382,1 +0.029670653342538315,0.013410279291146063,0.007739154744185145,0.03525325094429812,0.0683015321240196,1 +0.029688394341180042,0.013531388299878483,0.007845347582577326,0.0348618163996964,0.07145638692054852,1 +0.028899973428664863,0.012422351439892163,0.007944532313857429,0.03729833482789037,0.06857157821095569,1 +0.028642330939649383,0.012532955206273743,0.008268401920395687,0.03860168558915096,0.06760713367797054,1 +0.02774639319674014,0.012774020340170484,0.008352172655897923,0.034925236605098414,0.06724264378210047,1 +0.026304823847846773,0.012590456136499806,0.00821093900234711,0.03726509152601912,0.06703915896389048,1 +0.02895349495514236,0.012395445114275847,0.00795187549454805,0.03618371381118314,0.06416005741405079,1 +0.02716588343243874,0.011792420457820713,0.007867163759322131,0.036831322229036845,0.06416065305756077,1 +0.028396496179827584,0.012777531331846594,0.007460427256597901,0.036404788579494085,0.06244190902763199,1 +0.03162113251133527,0.011234709015775005,0.007115757045374369,0.036837005528701054,0.06342754407685468,1 +0.030967946075925293,0.012690003785266852,0.007163962921317712,0.03783138199653473,0.06166842607190356,1 +0.027991070113850883,0.014613069108428028,0.00791086181330948,0.037274293350935316,0.061472172657830164,1 +0.027829348301143426,0.010974196460820162,0.007844174743979661,0.038897943084677034,0.06253350650095299,1 +0.028638120119658633,0.018869856037433352,0.008569982253540413,0.03598363839970297,0.061508563823164546,1 +0.031224262915653476,0.01600067328489031,0.008457492569129874,0.03353757439010513,0.0584737904358998,1 +0.029162889713764156,0.015539235289595455,0.008556818652803597,0.03340417534321396,0.057857412289316235,1 +0.030399926881826092,0.015633945189282752,0.008625196799407674,0.031680914599784435,0.05461013593840272,1 +0.03007295809750326,0.016265158317431496,0.008208110286861754,0.03201220938372863,0.054585474513374546,1 +0.029145494712595404,0.01617134748788857,0.008621655154664131,0.03235679922033078,0.05307035075630029,1 +0.02993454165548579,0.01566309055053891,0.0083167853432706,0.03098720452115923,0.05138672656155876,1 +0.030016961744216503,0.015828564243696372,0.007709448607230175,0.030123640241223437,0.0520927960873281,1 +0.031169746287332673,0.01826572923982589,0.0075431365477480515,0.03269721899529904,0.04832862536202135,1 +0.03309759927998418,0.0179849136611591,0.010588464967995613,0.03266725471175236,0.047271252574825055,1 +0.03393033294381003,0.01822926630937454,0.01011162841562244,0.032640201450120386,0.04510834723180999,1 +0.03191834540382148,0.0201852636898023,0.009201380247455477,0.031773574645213895,0.04382123002669066,1 +0.032378885418298135,0.019676385857765176,0.00965055106783327,0.031611033965916455,0.04234188362261987,1 +0.032112748646096666,0.021388998764228968,0.009998838626738803,0.03131715943758866,0.04066104404131998,1 +0.03228422485975448,0.021874319948535014,0.010563921291460818,0.026756681173726992,0.04061580651875246,1 +0.03496069991551397,0.022650943698793065,0.01145211607333363,0.024057532259742567,0.04111177935744535,1 +0.031974760407104647,0.02298546502599533,0.012823068964646352,0.024743550524138652,0.03856808365270848,1 +0.03442037360264628,0.02275914072484236,0.012419717415269297,0.024937377269892255,0.037822032613999294,1 +0.03526594184617031,0.022907180494372723,0.012737098247845824,0.024233147538601583,0.03981911144437762,1 +0.03619078491724109,0.022627756840417304,0.012694703363619816,0.023906500877111107,0.03792415150797479,1 +0.03439134310563504,0.02282666798331887,0.01259896060441749,0.023321803745575038,0.03671762216028718,1 +0.03582384713402488,0.025231143688113706,0.01339185384683789,0.024669315233302628,0.038430541505343904,1 +0.0410589802973699,0.023932303493794348,0.014316053699916094,0.028145641376469143,0.04133934402726799,1 +0.04152370763899997,0.022740364220312635,0.014098823261814752,0.02799081329802119,0.04053597064047483,1 +0.04140558710213658,0.021190332772243234,0.015042253808038447,0.02998538032576498,0.04282438099149516,1 +0.03598445181915663,0.020772317496520795,0.016002127636119787,0.029607560626787176,0.044022841310365726,1 +0.039114853395210036,0.0233246244551926,0.01582441947409912,0.030224903687175713,0.04375838108737967,1 +0.040102761731009705,0.022763523232932306,0.014846199943482762,0.03075586391707389,0.044939659044116106,1 +0.038532490641661164,0.02412645158357554,0.014229644295650016,0.030252774274767434,0.045590515586250935,1 +0.040723449310333795,0.02348922003247745,0.015206336754366851,0.031366969275480716,0.04615091524783821,1 +0.04080324134114985,0.02208224698778013,0.015638876415144377,0.03214840484381194,0.04635261235183075,1 +0.04257162892226505,0.024640140821259364,0.015296818119149671,0.03168334602857702,0.04721955898474722,1 +0.0412656072845298,0.02390208892419852,0.015074493667674776,0.031028597985390125,0.04744811900153536,1 +0.0403138492558649,0.0204970556987665,0.01620169651717756,0.030709699118224087,0.04664353258433718,1 +0.03871161922451929,0.02101211938942131,0.014046093217478009,0.029886790745302108,0.047993404085570894,1 +0.03879238056572607,0.02114859869624478,0.013689511975484958,0.030310000271572744,0.04734193648477175,1 +0.03552877191737228,0.020740278116137545,0.013830664122175002,0.028758915690982916,0.04723387817096413,1 +0.03689676519243424,0.020412110297645853,0.01430864845068546,0.02955489164349729,0.04603145156655392,1 +0.03831612085871971,0.01922673054016495,0.014601304914943361,0.029691631865728402,0.04725606527624019,1 +0.03897491005873418,0.01976090809811502,0.014211296571650791,0.028979472233809467,0.04653816984108529,1 +0.037389280989447965,0.019358301215143116,0.012288147486896136,0.030596740101946948,0.04571454637425677,1 +0.03736484438562642,0.02005763019701117,0.012824912979395265,0.029378788176631736,0.045825221937305545,1 +0.03983839083475761,0.01962755074642221,0.01216897514281834,0.029884370854248027,0.04549962024867677,1 +0.03889422952666465,0.018874094046835228,0.012319240584664829,0.03069101637020534,0.045103229959719555,1 +0.0377316911010762,0.01911966320731347,0.012448453663596771,0.029292171672265808,0.04521129632586351,1 +0.03807117650064061,0.019152076822277823,0.01138711002971347,0.029746454166230302,0.04406086886353505,1 +0.03273376652420413,0.019332493723849993,0.010198573104214445,0.029285279504589458,0.04211809598542483,1 +0.028299047185708234,0.018201443348308234,0.011096644053271168,0.025350511050931016,0.04049235550392303,1 +0.02782248133954912,0.017688793939768462,0.011101725414288319,0.024689827749975825,0.03873331009345556,1 +0.029961965958825637,0.017479342513219125,0.010923140382596973,0.02328667607943993,0.034215159607031545,1 +0.02989812567498134,0.017087191419417082,0.009299553699079616,0.023652506177884978,0.03126103112076898,1 +0.02179566457251227,0.01623067348646569,0.00972809198116504,0.023127279643234914,0.03050750312595582,1 +0.022284439915188005,0.01396067202618847,0.00895064669771927,0.02331532335459573,0.03025472525397052,1 +0.0222384024835948,0.015341769289670697,0.009444140058398142,0.022745248009160907,0.03024303341184563,1 +0.02091934472143372,0.015971624920145068,0.00882505480474432,0.02202623783259335,0.030009687289956413,1 +0.022005342994668203,0.014731096299027698,0.008830680797650921,0.020542440903981087,0.02900009661336954,1 +0.02173924320146766,0.014003402776037387,0.008747904537737512,0.02073112910447243,0.02807893902149969,1 +0.021997473912306664,0.015734986289880324,0.009011565399592393,0.01985622373829012,0.027555512288674366,1 +0.021863964960471372,0.014684107362006258,0.008893272572623557,0.020483956044243294,0.027079054749855606,1 +0.022402218962399738,0.015597408206375372,0.009129843940891362,0.020576653666256457,0.027590050893780717,1 +0.021801428075319536,0.015778850931449182,0.009315682302099249,0.020623985611604834,0.02693269773850087,1 +0.022876497284832795,0.015035385983014347,0.009275192045844812,0.020518720119817185,0.026178039169812747,1 +0.022625733374453314,0.01576815144728837,0.009299704912423915,0.01958395600389344,0.026449154162876277,1 +0.020214693389632107,0.014127819471421221,0.009217397770109947,0.019580136070377806,0.02572346976434212,1 +0.01991267053909091,0.014093874789933179,0.008950889217191393,0.020858528962641395,0.02421228431688801,1 +0.01881417496705999,0.014410786551914169,0.00951048351405522,0.01826639295925603,0.024548816675248112,1 +0.019056195889655993,0.012597818791108599,0.008732933230807854,0.01789455384298257,0.028046534496498493,1 +0.01829911856093409,0.013106487725786925,0.008876829146812612,0.018038102687186815,0.03133956265386873,1 +0.01786946753672262,0.014130424430025226,0.008722403609707989,0.01990106559683761,0.032078711914592654,1 +0.01857129598047902,0.01275425440228618,0.009494460629550958,0.020774578789688775,0.034166122543778765,1 +0.018337272613058866,0.012875301016800159,0.009723850493349317,0.020547263414988866,0.037399343311319014,1 +0.01829433609394972,0.013589094198260548,0.008984355672435993,0.020816235018331557,0.03846240253730974,1 +0.01903557063825327,0.013019348974882916,0.008921658471574138,0.021436902879358916,0.03879835134072193,1 +0.01614722589034063,0.011374123947475551,0.00890944279592676,0.022182779489182923,0.04028183562770095,1 +0.015811500419257024,0.010939107237291051,0.008820858703213441,0.022536511553659228,0.03989390775248729,1 +0.015465698275861311,0.01105441072144139,0.00871650407474419,0.024304506073683407,0.04179452248536563,1 +0.01500784421380489,0.01028528500789358,0.008729497652789475,0.023215677427946935,0.043202427170370476,1 +0.013554004724362665,0.009343976948980497,0.00950252879898009,0.022867174629171805,0.04341893756001308,1 +0.014658632612626562,0.00973191624339121,0.008617784271811268,0.024448371215474288,0.044127233196556814,1 +0.014270569646628211,0.009875380944207311,0.008364074494792643,0.024608513377456846,0.04509663261338319,1 +0.013966841771136041,0.009730164327118977,0.00909814665986019,0.02506031032900006,0.04637714559444466,1 +0.014295531518517717,0.008459383390297512,0.010746274514520463,0.02599843483200101,0.04528836331797621,1 +0.010974853470510644,0.008763537616666166,0.008853065328019221,0.025834533378681346,0.04523233468451898,1 +0.01048743234913083,0.008160162839233152,0.009552655417223128,0.025390467507922423,0.04608255648853487,1 +0.010257028831911222,0.0083922083785858,0.009402713881083092,0.025584522725370498,0.046067450278729265,1 +0.009153584557695158,0.008861576315127934,0.009173617496941133,0.026154915773918708,0.04680620109195338,1 +0.013293952867782377,0.013765834564157448,0.009949331477544968,0.02724063099440092,0.053481550436592536,1 +0.018751656808657424,0.010500630351749795,0.010314580204588876,0.026155636795767566,0.05438237377680713,1 +0.020516076481633994,0.01078073652862346,0.010364828368926746,0.025098980100945995,0.05411207169736668,1 +0.02108814168519385,0.009500528671513221,0.009720600472530361,0.0247203539941169,0.0542764517617183,1 +0.020826042343012777,0.010014426359862305,0.010822015462646741,0.025677601852825013,0.05411285941368103,1 +0.02393256745805595,0.014872190334370046,0.009769527589713329,0.02703184226918543,0.05279834236125448,1 +0.02997757442774355,0.016783076516051623,0.009738059788938385,0.028356322787369596,0.05347250492924106,1 +0.029932212905649538,0.015913516524342364,0.010346262767992144,0.028745727929504652,0.05263302029132167,1 +0.028026764822289805,0.017604078552989216,0.01088696282466703,0.029396639779989554,0.05237122821974546,1 +0.026018867028084612,0.016510249142597966,0.010685418417796333,0.02834469815118714,0.05034560175607353,1 +0.03700363397255228,0.020977312690278756,0.010729144661912604,0.031591593859139594,0.05253013481950095,1 +0.04456490270109189,0.018719953478151828,0.010763891103812295,0.03091345660850084,0.05353042207880859,1 +0.04524855526896567,0.020199024000628747,0.012796822331092852,0.03108073734932246,0.05819667528643666,1 +0.050304624677268206,0.021185213521563988,0.011900791756855095,0.031120969103826127,0.0634636950867999,1 +0.04819704037806751,0.020637294587165754,0.012150597916664124,0.0329904249941561,0.06335132902990225,1 +0.05372396476209453,0.023228310025821475,0.01196144119153315,0.03398169452164704,0.06351603541009186,1 +0.05888552940763381,0.02107932702668401,0.012366900777416924,0.03552079051805553,0.06469426127885153,1 +0.05667973673890249,0.02103553291221017,0.01233213299287789,0.035036345462461864,0.06606304580655649,1 +0.05890988888497741,0.021658166260633986,0.012944021141974023,0.03702613848144753,0.06456771697644413,1 +0.0572939778631369,0.02157279278714424,0.012379470839610846,0.036295201967907385,0.06724207111526352,1 +0.059795423037737785,0.026769790545322213,0.013498942607669685,0.0359052975056066,0.0670918821450628,1 +0.06749539912643933,0.025060369504588727,0.012298873497517475,0.03722398703568408,0.0679967147996283,1 diff --git a/csvfiles/nouhadata_Meditation2024-12-19,11-24-40-242172.csv b/csvfiles/nouhadata_Meditation2024-12-19,11-24-40-242172.csv new file mode 100644 index 0000000..0e38400 --- /dev/null +++ b/csvfiles/nouhadata_Meditation2024-12-19,11-24-40-242172.csv @@ -0,0 +1,27 @@ +meditation,key +14.223045349121094,1 +14.298620223999023,1 +14.455609321594238,1 +14.584455490112305,1 +14.739079475402832,1 +14.76807689666748,1 +14.737985610961914,1 +14.604192733764648,1 +14.697515487670898,1 +14.588272094726562,1 +14.252388000488281,1 +11.461552619934082,1 +11.322392463684082,1 +11.059428215026855,1 +10.909423828125,1 +10.820115089416504,1 +10.788674354553223,1 +10.733466148376465,1 +10.780632972717285,1 +10.899450302124023,1 +11.05318832397461,1 +11.167481422424316,1 +11.075584411621094,1 +10.983272552490234,1 +10.867120742797852,1 +10.874994277954102,1 diff --git a/run.py b/run.py index 966ebd5..38c2fc6 100644 --- a/run.py +++ b/run.py @@ -79,9 +79,10 @@ def send_nouhadata(s, choice, mind, process): print_text = text_type[choice] print("タスク経過時間:", str(get_tick_time[1] - get_tick_time[0]), print_text) - if offLineMode == False and get_tick_time[1] - get_tick_time[0] >= PREP_TIME: + if offLineMode == False: data, address = s.recvfrom(1024) process.Receive_BrainWave(nouha=data, key=mind, address=address) + get_tick_time[1] = pygame.time.get_ticks() #アプリの終了時の処理 @@ -153,7 +154,8 @@ else: screen.fill(black) pygame.display.update() - send_nouhadata(s, choice, mind, process) + if get_tick_time[1] - get_tick_time[0] >= PREP_TIME: + send_nouhadata(s, choice, mind, process) if check_exit(s, choice, process): break screen.fill(black) @@ -168,7 +170,8 @@ if not load_and_play_sound(filename): continue while pygame.mixer.music.get_busy(): - send_nouhadata(s, choice, mind, process) + if get_tick_time[1] - get_tick_time[0] >= PREP_TIME: + send_nouhadata(s, choice, mind, process) if check_exit(s, choice, process): break screen.fill(black) @@ -180,7 +183,8 @@ load_and_play_sound(filename) print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {instruction}") while get_tick_time[1] - get_tick_time[0] <= TIME: - send_nouhadata(s, choice, mind, process) + if get_tick_time[1] - get_tick_time[0] >= PREP_TIME: + send_nouhadata(s, choice, mind, process) if check_exit(s, choice, process): break load_and_play_sound(filename = os.path.join(SOUND_DIR, "タスクを終了してください.wav"))