Related
I have a text file.
each block parameter is separated by a colon
WoodBlock : 0.886275, 0.607843, 0.25098 : 2, 2, 2 : true : -75.5656891, -11.899992, -416.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
RustedBlock : 0.639216, 0.635294, 0.647059 : 2, 2, 2 : true : -35.5656891, -11.899992, -424.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
StoneBlock : 0.639216, 0.635294, 0.647059 : 2, 2, 2 : true : -50.5656891, -11.899992, -425.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
MetalRod : 0.388235, 0.372549, 0.384314 : 1, 3, 1 : true : -51.5656891, -11.399992, -412.506866, 1, 0, 0, 0, 1, 0, 0, 0, 1 : 0, 0, 0 : 0, 0, 0
Each line contains information about a block. I want to make an algorithm that will work like this:
the first line is selected
the second parameter of the line is selected - the script does something with this parameter
the third parameter of the line is selected - the script does something with this parameter
the fourth parameter of the line is selected - the script does something with this parameter
select the fifth parameter of the line - the script does something with this parameter
etc.
I tried using gsub but I don't know how I can select a certain line or parameter in it
You can use string.gmatch to match all parameters in the colon-delimited string. The pattern used to indicate the separator is [^:]+, which translates to "match everything but : (a colon)".
Here's an example script that loops over each line, then prints each parameter.
for line in io.lines("blocks.txt") do
for param in string.gmatch(line, "[^:]+") do
print(param)
end
end
My aim is to extract frames out of a video but many times the frames are inverted. This is happening because "VideoCapture" in cv2 does read the rotate flag data present in the video metadata. I am running it on mac and installed "ffmpeg" package (5.0.1) using brew package manager. This is my code to check rotation present in a video using ffmpeg.
import ffmpeg
import cv2
import pprint
pp = pprint.PrettyPrinter(depth=4)
def check_rotation(path_video_file):
# this returns meta-data of the video file in form of a dictionary
meta_dict = ffmpeg.probe(path_video_file)
pp.pprint(meta_dict)
# from the dictionary, meta_dict['streams'][0]['tags']['rotate'] is the key
# we are looking for
rotateCode = None
if int(meta_dict['streams'][0]['tags']['rotate']) == 90:
rotateCode = cv2.ROTATE_90_CLOCKWISE
elif int(meta_dict['streams'][0]['tags']['rotate']) == 180:
rotateCode = cv2.ROTATE_180
elif int(meta_dict['streams'][0]['tags']['rotate']) == 270:
rotateCode = cv2.ROTATE_90_COUNTERCLOCKWISE
return rotateCode
Here the meta_dict is supposed to have information about the rotation but it is not there.
printing out meta_dict gives:
{'format': {'bit_rate': '18498544',
'duration': '3.584000',
'filename': 'video.mp4',
'format_long_name': 'QuickTime / MOV',
'format_name': 'mov,mp4,m4a,3gp,3g2,mj2',
'nb_programs': 0,
'nb_streams': 2,
'probe_score': 100,
'size': '8287348',
'start_time': '0.000000',
'tags': {'com.android.version': '11',
'compatible_brands': 'isommp42',
'creation_time': '2021-12-08T10:56:57.000000Z',
'major_brand': 'mp42',
'minor_version': '0'}},
'streams': [{'avg_frame_rate': '990000/34997',
'bit_rate': '18804651',
'bits_per_raw_sample': '8',
'chroma_location': 'left',
'closed_captions': 0,
'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
'codec_name': 'h264',
'codec_tag': '0x31637661',
'codec_tag_string': 'avc1',
'codec_type': 'video',
'coded_height': 1080,
'coded_width': 1920,
'color_primaries': 'bt470bg',
'color_range': 'pc',
'color_space': 'bt470bg',
'color_transfer': 'smpte170m',
'display_aspect_ratio': '16:9',
'disposition': {'attached_pic': 0,
'captions': 0,
'clean_effects': 0,
'comment': 0,
'default': 1,
'dependent': 0,
'descriptions': 0,
'dub': 0,
'forced': 0,
'hearing_impaired': 0,
'karaoke': 0,
'lyrics': 0,
'metadata': 0,
'original': 0,
'still_image': 0,
'timed_thumbnails': 0,
'visual_impaired': 0},
'duration': '3.499700',
'duration_ts': 314973,
'extradata_size': 35,
'field_order': 'progressive',
'film_grain': 0,
'has_b_frames': 0,
'height': 1080,
'id': '0x1',
'index': 0,
'is_avc': 'true',
'level': 40,
'nal_length_size': '4',
'nb_frames': '99',
'pix_fmt': 'yuvj420p',
'profile': 'High',
'r_frame_rate': '120/1',
'refs': 1,
'sample_aspect_ratio': '1:1',
'side_data_list': [{...}],
'start_pts': 3168,
'start_time': '0.035200',
'tags': {'creation_time': '2021-12-08T10:56:57.000000Z',
'handler_name': 'VideoHandle',
'language': 'eng',
'vendor_id': '[0][0][0][0]'},
'time_base': '1/90000',
'width': 1920},
{'avg_frame_rate': '0/0',
'bit_rate': '128986',
'bits_per_sample': 0,
'channel_layout': 'stereo',
'channels': 2,
'codec_long_name': 'AAC (Advanced Audio Coding)',
'codec_name': 'aac',
'codec_tag': '0x6134706d',
'codec_tag_string': 'mp4a',
'codec_type': 'audio',
'disposition': {'attached_pic': 0,
'captions': 0,
'clean_effects': 0,
'comment': 0,
'default': 1,
'dependent': 0,
'descriptions': 0,
'dub': 0,
'forced': 0,
'hearing_impaired': 0,
'karaoke': 0,
'lyrics': 0,
'metadata': 0,
'original': 0,
'still_image': 0,
'timed_thumbnails': 0,
'visual_impaired': 0},
'duration': '3.584000',
'duration_ts': 172032,
'extradata_size': 2,
'id': '0x2',
'index': 1,
'nb_frames': '168',
'profile': 'LC',
'r_frame_rate': '0/0',
'sample_fmt': 'fltp',
'sample_rate': '48000',
'start_pts': 0,
'start_time': '0.000000',
'tags': {'creation_time': '2021-12-08T10:56:57.000000Z',
'handler_name': 'SoundHandle',
'language': 'eng',
'vendor_id': '[0][0][0][0]'},
'time_base': '1/48000'}]}
Can someone please help me to find if rotation is present in a video?
Disclaimer, I'm a beginner.
I have an array that is 16 digits, limited to 0's and 1's. I'm trying to create a new array that contains only the index values for the 1's in the original array.
I currently have:
one_pos = []
image_flat.each do |x|
if x == 1
p = image_flat.index(x)
one_pos << p
image_flat.at(p).replace(0)
end
end
The image_flat array is [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
With the code above, one_pos returns [3, 3] rather than the [3, 5] that I'd expect.
Where am I going wrong?
Where am I going wrong?
When you call
image_flat.index(x)
It only returns first entry of x in image_flat array.
I guess there are some better solutions like this one:
image_flat.each_with_index do |v, i|
one_pos << i if v == 1
end
Try using each_with_index (http://apidock.com/ruby/Enumerable/each_with_index) on your array.
image_flat = [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
one_pos = []
image_flat.each_with_index do |value, index|
if value == 1
one_pos << index
end
end
I think this is the most elegant solution here:
image_flat.each_index.select{|i| image_flat[i] == 1}
Here is a solution if you are looking for a solution that doesn't reach out of the enumerable block although it does require a chained solution.
image_flat.each_with_index.select { |im,i| im==1 }.map { |arr| arr[1] }
Its chained and will require an additional lookup so Gena Shumilkin's answer will probably be more optimal for larger arrays.
This was what I originally thought Gena Shumilkin was trying to reach until I realized that solution used each_index instead of each_with_index.
I want to add a line break (\n) in front of the 5th element on every other line:
2, 0, 0, 0, 2
4, 0, 0, 0, 4
6, 0, 0, 0, 6
8, 0, 0, 0, 8
... in order to get:
2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8
What I have so far in gawk doesn't work:
gawk '{if (NR % 2) {$5=\n$5; print} else print}'
You could say:
awk '{NR%2 || $5="\\n"$5 }1' filename
Note that you'll need to escape the \ in order to get a literal \.
For your input, it'd produce:
2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8
Alternatively, (as pointed out by #WilliamPursell), you could say:
awk '!(NR%2) {$5="\\n"$5 }1' filename
OK this is the case, I have the following query
INSERT INTO 'FoodListTBL' ('AutoNo','CHOCals','PrtCals', 'FatCals','CHOgram','PrtGram','FatGram','CatId', 'TimeTypeId','TotalCals','Visibl', 'IsActive','NameAr','NameEn','CountryId', 'TotalPerUnit','UnitId','PreferedBread1', 'PreferedMilk1','PreferedVeg1','PreferedFat1', 'PreferedFruit1','CauseAllergy','AllergyCatId', 'TotalLikes','NameDescEn','NameDescAr','ChoPerUnit','PrtPerUnit','FatPerUnit','Quantity','PreferedBread2','PreferedMilk2', 'PreferedVeg2','PreferedFat2', 'PreferedFruit2','IV','UV','InsertDate','InsertUser') VALUES (818,0,0, 45, 0, 0, 5, 17, 1, 45,1, 1, 'زبدة قليلة الدسم', 'Butter reduced fat', 0, 45, 14, 0, 0, 0, 0, 0, 0, 0, 0, 'Butter reduced fat', 'زبدة قليلة الدسم', 0, 0, 45, 1, 0, 0, 0, 0, 0, 492, 0, '-', '-' ),(819,0,0, 45, 0, 0, 5, 17, 1, 45,1, 1, 'زبدة', 'Butter regular', 0, 45, 4, 0, 0, 0, 0, 0, 0, 0, 0, 'Butter regular', 'زبدة', 0, 0, 45, 1, 0, 0, 0, 0, 0, 493, 1475, '-', '-')
this query executed successfully on iOS 6.X and failed on any iOS less than 5.X taking into consideration that any other insert query on other tables finished successfully on any iOS
and I've tried two codes for insert this is one of them
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSLog( #"Error while inserting data: '%s'", sqlite3_errmsg(database));
else NSLog(#"New data inserted");
sqlite3_reset(compiledStatement);
}else
{
NSLog( #"Error while inserting '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement)
and the result in the two cases is
Error while inserting 'near ",":syntax error'
aging this query is functional on every thing except iOS < 6.0
any clues are appreciated
SQLite before version 3.7.11 does not support the multi-record INSERT syntax.
Use multiple INSERT commands, or insert the records with INSERT ... SELECT ... UNION ALL ....